Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.

Similar presentations


Presentation on theme: "Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply."— Presentation transcript:

1 Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.

2 syntax switch ( ) { case value: // Code to execute if == // value break; case value: // Code to execute if == // value break;... default: // Code to execute if does not equal the value following any of the cases break; }

3 The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions.

4 Sadly, it isn't legal to use case like this: int a = 10; int b = 10; int c = 20; switch ( a ) { case b: // here should be an actual value, such as 10, not a varible // Code break; case c: // Code break; default: // Code break; }

5 The default case is optional, but it is wise to include it as it handles any unexpected cases. Switch statements serves as a simple way to write long if statements when the requirements are met. Often it can be used to process input from a user.

6 #include using namespace std; int main() { int input; float num1, num2; cout<<“Enter two numbers\n”; cin>>num1>>num2; cout<<“Enter 1. To add\n 2. Subtract\n3. multiply\n"; cin>> input; switch ( input ) { case 1: // if user enter 1 then do the following result = num1+nume2; // break; case 2: // if user enter 2 then do the following result = num1- num2; break; case 3: // if user enter 3 then do the following result = num1 * num2; break; default: // if user enter any number other than 1, 2,and 3, do the following cout<<"Error, bad input, quitting\n"; break; } // end of switch return 0; } // end of main

7 Project to do Write a program that ask user to enter a grade (0- 100) and assign letter grade (use switch structure) A 90 – 100 B 80 – 89 C 70 – 79 D 60 -69 F 0 – 59 Print out the letter grade


Download ppt "Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply."

Similar presentations


Ads by Google