Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4, cont: Control Structures if else nested if switch.

Similar presentations


Presentation on theme: "Chapter 4, cont: Control Structures if else nested if switch."— Presentation transcript:

1

2 Chapter 4, cont: Control Structures if else nested if switch

3 Conditional The Conditional Statement Syntax if (expression) statement1 else statement2 if (a > b) max = a; else max = b; * * * Use block or compound statements for either or both

4 Conditional The Conditional Statement if (a > b) max = a; else max = b; end of statement for if clause else => not done with the if yet end of statement for else clause and end of the entire if statement if (expression) tells compiler to look for a complete statement single (semicolon) or block {braces} Then look for else – if an else is not found, continue with next statement If else is found, this is the other alternative to the last if - So, look for the required statements

5 Finding the Minimum of Three Values * * * int x, y, z, min; cout << “Input three integers: “; cin >> x >> y >> z; if (x < y) min = x; else min = y; if (z < min) min = z; cout << “The minimum value is “ << min << ‘\n’;

6 Output: Input three integers: Interactive Program Finding the minimum of three values * 9 5 -12 The minimum value is -12 _

7 if Nested if Statements A nested if statement is an if statement that is included within another if statement. Syntax if (expression1) { if (expression2) statement } If (a < b) If a > 3 cout << a; Braces are essential

8 if Nested if Example cout <<“Enter guess”; cin >> number if (number != secretnumber) { cout << “Sorry, that’s not the number.\n”; if (number > secretnumber) cout << “You guessed too high.\n”; else cout << “You guessed too low.\n”; } *

9 2 statement1 3 else if (expression2) 4 statement2 5... 6 else if (expressionN) 7 statementN 8 else 9 last statement 10 next statement * if...else Chained if...else Example Syntax 1 if (expression1)

10 if...else Chained if...else Example if (monthlySales >= 50000) income = 375 +.16 * monthlySales; else if (monthlySales >= 40000) income = 350 +.14 * monthlySales; else if (monthlySales >= 30000) income = 325 +.12 * monthlySales; else if (monthlySales >= 20000) income = 300 +.09 * monthlySales; else if (monthlySales >= 10000) income = 250 +.05 * monthlySales; else income = 200 +.03 * monthlySales ; next statement *

11 switch The switch Statement « Similar to cascading if statements « Can list any number of branches « Used in place of nested if statements « Avoids confusion of deeply nested ifs « For cases that compare the value of an integer expression to a specific value

12 switch The switch Statement Syntax switch (expression) { case value1: statement1; break; case value2: statement2; break;  case valueN: statementn; break; default: statement; } no ; use : * case value2: case value3: statement2; break; Multiple cases can have same action.

13 switch The switch Statement Syntax switch (expression) { case value1: statement1; break; case value2: statement2; break;  case valuen: statementn; break; default: statement; } no ; use : If none of the cases matched…

14 switch The switch Statement switch (letterGrade) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; cont.

15 switch The switch Statement case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; } next statement

16 switch The switch Statement switch (letterGrade) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; } Mutually exclusive, ordinal values

17 break The break Statement switch (letterGrade) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; } Assume letterGrade = ‘D’ Print this  Then this 

18 break The break Statement switch (letterGrade) { case ‘A’: case ‘B’:cout << “Good Work”; break; case ‘C’:cout << “Average Work”; break; case ‘D’: case ‘E’:cout << “Poor Work”; }

19 break The break Statement switch (letterGrade) { case ‘A’: case ‘a’: case ‘B’: case ‘b’:cout << “Good Work”; break; case ‘C’: case ‘c’:cout << “Average Work”; break; etc. } Remember: use default clause to catch programming and input errors

20 switch The switch Statement * * * * Menu * * * * 1. NY Yankees 2. Indians 3. Dodgers Choose either 1, 2, or 3:

21 switch The switch Statement switch (choice) { case 1: cout << “World Champs”; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; } What will be the output?

22 switch The switch Statement What will be the output when the user enters 1 World ChampsGood GuysDa Bums 2 Good GuysDa Bums 3 Da Bums * * * * 4 skips the switch

23 switch The switch Statement * switch (choice) { case 1: cout << “World Champs”; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; } switch (choice) { case 1: cout << “World Champs”; break; case 2: cout << “Good Guys”; break; case 3: cout << “Da Bums”; break; default: cout << “Enter a 1, 2, or 3”; }

24 Example from text, page 157 Switch(Choice) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’: cout << “\nThe character in Choice is a vowel” <<endl; break; default: cout << “\nThe character in Choice is not a vowel” <<endl; break; //this break is optional } // end of switch statement

25 else The Dangling else if (avg >= 60.0) if (avg < 70.0) cout << “Passing, but marginal”; else cout << “Failing”; * if (avg >= 60.0) { if (avg < 70.0) cout << “Passing, but marginal”; } else cout << “Failing”; else will be paired with nearest if

26 Common Errors Using = in place of == Improper braces in nested ifs Too deeply nested ifs Missing “cases” Dangling “else” Missing break statements in switch statement Copyright © 1999 by Freedom TLC, Inc.

27 Debugging Syntax errors vs. Logic error Prevention - plan first! Display values C++ Debugger: man gdb Copyright © 1999 by Freedom TLC, Inc.

28 “I discovered I always have choices, and sometimes it’s only a choice of attitude” Judith M. Knowlton Copyright © 1999 by Freedom TLC, Inc.


Download ppt "Chapter 4, cont: Control Structures if else nested if switch."

Similar presentations


Ads by Google