switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character : Char or Integer ? * EOF * Case Study * Exercise/Home Work Dr. Ming Zhang
General Form of a switch Statement switch (expression) { case value_1: // terminated with a colon statement_1; break; // exit from switch; optional case value_2: statement_m; break; default: // last else; optional statement_aa;......} Dr. Ming Zhang
Details of switch Statement (1) * switch The keyword switch identifies the start of the switch. * expression -The expression in parentheses is evaluated and the result of the expression compared to various alternative values contained within the compound statement. -The expression must evaluate to an integer result or a compilation error results. Dr. Ming Zhang
Details of switch Statement (2) * case -The keyword case is used to identify or label individual values that are compared to the value of the switch expression. -The switch expression’s value is compared to each of these case value in order in which these values are listed until a match is found. -When the mach occurs, execution begins with the statement immediately following the match. Dr. Ming Zhang
Details of switch Statement (3) * default -If the value of the expression does not match any case values, however, no statement is executed unless the keyword default is encountered. - The word default is option and operates the same as the last else in an if-else chain. -If the value of the expression does not match any of the case values, program execution begins with the statement following the word default. Dr. Ming Zhang
Details of switch Statement (4) * break -Once an entry point has been located by the switch statement, all further case evaluations are ignored and execution continues through the end of the compound statement unless a break statement is encountered. - This is the reason for the break statement, which identifies the end of a particular case and causes an immediate exit from the switch statement. - If the break statements are omitted, all cases following the matching case value, including the default case, are executed. Dr. Ming Zhang
Flowchart of switch Statement case a case a action(s) break case b case b action(s) break case z case z action(s) break default action(s) Dr. Ming Zhang
Example of switch Statement int num1 = 10, num2 = 10; cin >> opselect ; switch (opselect) { case 1: cout << “The sum:”<< (num1+num2); break; case 2: cout<<“The product”<<(num1*num2); break; case 3: cout<<“The division”<<(num1/num2); break; } Dr. Ming Zhang
cin.get * grade = cin.get The cin.get( ) function reads one character from the keyboard and stores that character in integer variable grade. * Details will be introduced in Chapter 6, “Classes”. Dr. Ming Zhang
Char or Integer * char or integer Characters normally are stored in variables of type char; however, an important feature of C++ is that characters can be stored in any integer data type because they are represented as 1-byte integers in the computer. * cout (‘a’); output: a has the value 97 * ‘a’: integer value of “a”. Dr. Ming Zhang
Assignment Statements * Example of Assignment Statements a=4; b=2; a = b = c = 0; a=4 or 0 ??????? b=2 or 0 ??????? * The assignment operator associated from right to left. The variable b is then assigned the value of the assignment c = 0 (which is also 0). Same reason, a = 0. Dr. Ming Zhang
EOF * EOF (end-of-file) EOF is the symbol whose acronym stands for end-of-file. * EOF normal has the value -1. However, we do not type the value -1 nor do we type the letters EOF. Rather, you type a system- dependent keystroke combination to mean “end-of-file”. * UNIX: -> EOF MS-DOS: -> EOF Dr. Ming Zhang
Case Study - Figure 2.22 (D & D) while ( ( grade = cin.get( ) ) != EOF) { switch (grade) { case ‘A’: case ‘a’: ++aCount; break; } Dr. Ming Zhang
Question 1 - Exercise/Home Work Rewrite the following if-else chain using a switch statement: if (res_type == 1) data( ); else if (res_type == 2) capacity( ); else if (res_type == 3) volume( ); else if (res_type == 4) area( ); else if (res_type == 5) files( ); else retrieve( ); Dr. Ming Zhang
Question 2 - Exercise/Home Work Write a program to use a switch statement to select the arithmetic operation (addition, multiplication, or division) to be performed on two numbers depending on the value of the variable opselect. A menu for your operation message should be created. Dr. Ming Zhang