THE SWITCH-CASE STATEMENT

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

(THE SWITCH-CASE STATEMENT):

(THE SWITCH-CASE STATEMENT)

(THE SWITCH-CASE STATEMENT):

(THE SWITCH-CASE STATEMENT) The switch-case is a good alternative to cascading if-else statements. The syntax for the switch-case statement is as follows: switch(<expression>) { case <label-1> : <statement-1>; [break;] case <label-2> : <statement-2>; [break;] … case <label-n>: <statement-n>; [break] [default : <statement-d>; ] }

PowerPoint Presentation:

The expression may be an integer or character variable or, as the name suggests, an expression that evaluates to an integer or a character value the use of float and double data type values will result into an error. The <expression> is evaluated and the value compared is with each of the case labels. The case labels must have the same type as the <expression> and they must all be different. If a match is found between the selector and one of the case labels, say <label-1>, then the statement from <statement-1> will be executed. The same applies to other cases.

PowerPoint Presentation:

The [break] statement is optional. If it is present, it will cause the program to “break” or “jump” out of the switch-case, and to execute the next statement following switch-case. If the break is not present, it will cause the program to execute the statement in the following case, i.e., <statement- 2> above, causing a waterfall effect , the same behavior applies to the other cases .

PowerPoint Presentation:

If the value of the expression does not match with any of the case labels then the statement <statement-d> associated with [default] is executed. The [default] is optional but it should only be left out if it is certain that the expression will always take the value of one of the case labels . Note that the statement associated with a case label can be a single statement or a sequence of statements (without being enclosed in curly brackets).

Example #1:

Example #1 #include < iostream > using namespace std; void main(void) { int month; cout << "Input month (1-12): "; cin >> month; switch(month) { case 1: cout << "It is cold season.\n"; break; case 2: cout << "It is cold season.\n"; break; case 3: cout << "It is summer season.\n"; break; case 4: cout << "It is summer season.\n"; break; case 5: cout << "It is summer season.\n"; break; case 6: cout << "It is rainy season.\n"; break; case 7: cout << "It is rainy season.\n"; break; case 8: cout << "It is rainy season.\n"; break; case 9: cout << "It is rainy season.\n"; break; case 10: cout << "It is rainy season.\n"; break; case 11: cout << "It is cold season.\n"; break; case 12: cout << "It is cold season.\n"; break; } }

“Waterfall Effect” Example:

“Waterfall Effect” Example #include <iostream> using namespace std; void main(void) { int month; cout << "Input month: "; cin >> month; switch(month) { case 1: case 2: case 11: case 12: cout << "It is cold season.\n"; break; case 3: case 4: case 5: cout << "It is summer season.\n"; break; case 6: case 7: case 8: case 9: case 10: cout << "It is rainy season.\n"; break; default: cout << "Input month out of range.\n"; } }

authorStream Live Help