+ All Categories
Home > Documents > Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch...

Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch...

Date post: 18-Jan-2018
Category:
Upload: shavonne-waters
View: 228 times
Download: 0 times
Share this document with a friend
Description:
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

If you can't read please download the document

Transcript

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 expressions 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


Recommended