Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Control Structure: Switch Case Statement

Similar presentations


Presentation on theme: "Selection Control Structure: Switch Case Statement"— Presentation transcript:

1 Selection Control Structure: Switch Case Statement

2 Introduction Switch case statement do not require the evaluation of a logical expression, compare with if else statement. Give the computer power to select from among many alternatives/options. The reverse words are: switch, case, break, default. Switch structure contain constant expression. Constant expression will be evaluated at first in the structure. It is used to select the action/option specified in the statement that follow the reserved word case. Normally, constant expression is the identifier. In such situation, it is also called as selector. Break is used to immediately exit from the switch structure.

3 General Form switch(condition){ case constant_expression: { statements; break; } default: single line statement; // no compound statement //if more than single line statement MUST have compound //statement

4 Example 1 – if…else statement using int
int num; cout << “Please select your menu number”; cin >> num; //condition statement (variable) if (num == 1){ //number 1 & 2 known as constant expression cout << “You have select menu number 1”; } else if(num == 2){//number 1 & 2 known as constant expression cout << “You have select menu number 2”; else cout << “Invalid selection”;

5 Example 1 – Constant Expression using int
int num; cout << “Please select your menu number”; cin >> num; //condition statement for switch (variable) switch(num){ case 1:{ //number 1 & 2 known as constant expression cout << “You have select menu number 1”; break; } case 2:{//number 1 & 2 known as constant expression cout << “You have select menu number 2”; default: cout << “Invalid selection”;

6 Example 2 – if…else statement using char
char character; cout << “Please select your menu”; cin >> character; //condition statement (variable) if (character == ‘A’){ //A & B known as constant expression cout << “You have select menu A”; } else if(character == ‘B’){//A & B known as constant expression cout << “You have select menu B”; else cout << “Invalid selection”;

7 Example 2 – Constant Expression using char
char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) switch(character){ case ‘A’:{ //A & B known as constant expression cout << “You have select menu A”; break; } case ‘B’:{//A & B known as constant expression cout << “You have select menu B”; default: cout << “Invalid selection”;

8 Example 3 – if…else statement using char
char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) if(character == ‘A’ || character == ‘a’){ //A & B known as constant expression cout << “You have select menu A”; } else if(character == ‘B’ || character == ‘b’) {//A & B known as constant expression cout << “You have select menu B”; else cout << “Invalid selection”;

9 Example 3 – Constant Expression using char
char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) switch(character){ case ‘A’: case ‘a’:{ //A & B known as constant expression cout << “You have select menu A”; break; } case ‘B’: case ‘b’:{//A & B known as constant expression cout << “You have select menu B”; default: cout << “Invalid selection”;

10 Advantages & Disadvantages
Suitable for menu selection. Proper code arrangement. Do not require the evaluation of logical expression. Disadvantage: Not suitable for comparing string (strcmp). Not suitable for comparing range of numbers.


Download ppt "Selection Control Structure: Switch Case Statement"

Similar presentations


Ads by Google