Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Lecture 7 Control Structure I (Selection) – Part II

Similar presentations


Presentation on theme: "C++ Programming Lecture 7 Control Structure I (Selection) – Part II"— Presentation transcript:

1 C++ Programming Lecture 7 Control Structure I (Selection) – Part II
By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

2 The Hashemite University
Outline Introduction. switch statement. Examples. The Hashemite University

3 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

4 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

5 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

6 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 aCount = 0, // number of A's bCount = 0, // number of B's cCount = 0, // number of C's dCount = 0, // number of D's fCount = 0; // number of F's 17 18 cout << "Enter the letter grades." << endl << "Enter the EOF character to end input." << endl; 20 21 while ( ( grade = cin.get() ) != EOF ) { 22 switch ( grade ) { // switch nested in while 24 case 'A': // grade was uppercase A case 'a': // or lowercase a aCount; break; // necessary to exit switch 29 case 'B': // grade was uppercase B case 'b': // or lowercase b bCount; break; 34 Notice how the case statement is used

7 Notice the default statement.
case 'C': // grade was uppercase C case 'c': // or lowercase c cCount; break; 39 case 'D': // grade was uppercase D case 'd': // or lowercase d dCount; break; 44 case 'F': // grade was uppercase F case 'f': // or lowercase f fCount; break; 49 case '\n': // ignore newlines, why we need to process it??? case '\t': // tabs, case ' ': // and spaces in input break; 54 default: // catch all other characters cout << "Incorrect letter grade entered." << " Enter a new grade." << endl; break; // optional } 60 } 61 62 cout << "\n\nTotals for each letter grade are:" << "\nA: " << aCount << "\nB: " << bCount << "\nC: " << cCount << "\nD: " << dCount << "\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.

8 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

9 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

10 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

11 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

12 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

13 The Hashemite University
Additional Notes This lecture covers the following material from the textbook: Fourth Edition: Chapter 2: Section 2.16 The Hashemite University


Download ppt "C++ Programming Lecture 7 Control Structure I (Selection) – Part II"

Similar presentations


Ads by Google