Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.

Similar presentations


Presentation on theme: "C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department."— Presentation transcript:

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

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

3 The Hashemite University3 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.

4 The Hashemite University4 The switch Multiple-Selection Structure switch 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. switch ( variable or expression ) { case value1: //do something; break; case value2: //do something; break; case value3: //do something; break; case value4: //do something; break; default: // do something break; }

5 The Hashemite University5 Flowchart Representation of switch true false...... case a case a action(s)break case b case b action(s)break false case z case z action(s)break true default action(s)

6 1// Fig. 2.22: fig02_22.cpp 2// Counting letter grades 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9int 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 Ctrl z

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

8 Program Output Enter the letter grades. Enter the EOF character to end input. a B c C A d f C E Incorrect letter grade entered. Enter a new grade. D A B ^Z Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1

9 The Hashemite University9 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.

10 The Hashemite University10 Notes II switch is used for testing 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).

11 The Hashemite University11 Differences between cin>> and cin.get() I cin>> the entered value must be in 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.

12 The Hashemite University12 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 97 (ASCII representation)

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


Download ppt "C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department."

Similar presentations


Ads by Google