C++ Programming Lecture 7 Control Structure I (Selection) – Part II By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department
The Hashemite University Outline Introduction. switch statement. Examples. The Hashemite University
The Hashemite University Introduction In this lecture we will study the last control structure in C++. This control structure is associated with selection. It is used when there are multiple selection choices, i.e. more than 2. It can be used instead of nested if/else structure. The Hashemite University
The switch Multiple-Selection Structure Useful when variable or expression is tested for multiple values. Consists of a series of case labels and an optional default case. Used instead of nested if/else statements to make the code more readable and easier to trace. The Hashemite University
Flowchart Representation of switch true false . case a case a action(s) break case b case b action(s) case z case z action(s) default action(s) The Hashemite University
Notice how the case statement is used 1 // Fig. 2.22: fig02_22.cpp 2 // Counting letter grades 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main() 10 { 11 int grade, // one grade 12 aCount = 0, // number of A's 13 bCount = 0, // number of B's 14 cCount = 0, // number of C's 15 dCount = 0, // number of D's 16 fCount = 0; // number of F's 17 18 cout << "Enter the letter grades." << endl 19 << "Enter the EOF character to end input." << endl; 20 21 while ( ( grade = cin.get() ) != EOF ) { 22 23 switch ( grade ) { // switch nested in while 24 25 case 'A': // grade was uppercase A 26 case 'a': // or lowercase a 27 ++aCount; 28 break; // necessary to exit switch 29 30 case 'B': // grade was uppercase B 31 case 'b': // or lowercase b 32 ++bCount; 33 break; 34 Notice how the case statement is used
Notice the default statement. 35 case 'C': // grade was uppercase C 36 case 'c': // or lowercase c 37 ++cCount; 38 break; 39 40 case 'D': // grade was uppercase D 41 case 'd': // or lowercase d 42 ++dCount; 43 break; 44 45 case 'F': // grade was uppercase F 46 case 'f': // or lowercase f 47 ++fCount; 48 break; 49 50 case '\n': // ignore newlines, why we need to process it??? 51 case '\t': // tabs, 52 case ' ': // and spaces in input 53 break; 54 55 default: // catch all other characters 56 cout << "Incorrect letter grade entered." 57 << " Enter a new grade." << endl; 58 break; // optional 59 } 60 } 61 62 cout << "\n\nTotals for each letter grade are:" 63 << "\nA: " << aCount 64 << "\nB: " << bCount 65 << "\nC: " << cCount 66 << "\nD: " << dCount 67 << "\nF: " << fCount << endl; 68 69 return 0; 70 } break causes switch to end and the program continues with the first statement after the switch structure. Notice the default statement.
Program Output Enter the letter grades. Enter the EOF character to end input. a B c C A d f E Incorrect letter grade entered. Enter a new grade. D ^Z Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1 Program Output
The Hashemite University Notes I default statement is optional in switch. In the previous example variable grade is called the controlling expression. If no break statement is included, all case statements will be implemented once a match has been found. Logical errors: Forgetting break statement. Forgetting white space between case and the value to test against it( e.g. case3:). This will create a label. default and case statements can be placed in any order inside the switch structure. No braces are required around multiple statements for the same case structure in switch. break determine the end of the case code block. Identical case labels in switch is a syntax error. The Hashemite University
The Hashemite University Notes II switch is used for testing constant integral expressions only, i.e. float, arrays, strings are not allowed. Only integer values or single character values. These values can be the result of an expression, variable, or a constant value. (x + 5) for example is allowed. For case statements only constants integer values are allowed (either integer or single characters). No expressions, float/double values, and variables are allowed. What to do when you want to test double values??? (nested if/else statements). The Hashemite University
Differences between cin>> and cin.get() I the entered value must be consistence with the variable data type. can be used to read any data type from the keyboard. will stop at the first white space when you enter a complete string or when you press enter. cin.get() cin.get() read only one character at a time. Do not stop at white spaces. If the variable is not of type char, cin.get() will store the ASCII representation of the entered character (even if it is a numeral). If you entered more than one character it will store the first entered character only. The Hashemite University
Differences between cin>> and cin.get() II E.g: int x = 10; cin>>x; // the user entered character a cout <<x; //output is 0. x = cin.get(); // the user entered character a cout<<x; //output is 61 (ASCII representation of a in hexadecimal) The Hashemite University
The Hashemite University Additional Notes This lecture covers the following material from the textbook: Fourth Edition: Chapter 2: Section 2.16 The Hashemite University