Selection Control Structure: Switch Case Statement
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.
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
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”;
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”;
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”;
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”;
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”;
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”;
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.