Presentation is loading. Please wait.

Presentation is loading. Please wait.

(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.

Similar presentations


Presentation on theme: "(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University."— Presentation transcript:

1 (4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University

2 C. Hundhausen, A. O’Fallon 2 Nested if statements (1) Consider the following scenario: A high school baseball team awards merit points to players based on their offensive performance. A single (encoded 's') is worth 1 point, a double (encoded 'd') is worth 2 points, a triple (encoded 't') is worth 3 points, and a home run (encoded 'h') is worth 4 points. Any at-bat that leads to an out (encoded 'o') worth 0 points. Write a C statement that, given an at-bat character, properly awards points.

3 C. Hundhausen, A. O’Fallon 3 Nested if statements (2) We can write a nested if statement to handle this situation: char at_bat; int points; printf("Enter the at-bat code (s,d,t,h,o): "); scanf(" %c",&at_bat); if (at_bat == 's') /* single */ { points = 1; } else if (at_bat == 'd') /* double */ { points = 2; } else if (at_bat == 't') /* triple */ { points = 3; } else if (at_bat == 'h') /* home run */ { points = 4; } else /* out */ { points = 0; }

4 C. Hundhausen, A. O’Fallon 4 Nested if statements (3) Consider the following updated scenario: A high school baseball team awards merit points to players based on their offensive performance and the class standing ('f' = freshman, 'o' = sophomore, 'j' = junior, and 's' = senior). In particular, freshmen and sophomores earn an extra point for home runs, whereas juniors and seniors do not earn any points for singles. Write a C if-statement that, given an at-bat character and a class standing character, properly awards points.

5 C. Hundhausen, A. O’Fallon 5 Nested if statements (4) We can write an even more nested if statements to handle this situation: char at_bat, class_standing; int points; … if (at_bat == 's') { /* single */ if (class_standing == 'f') || (class_standing == 'o') { points = 1; } else { points = 0; } } else if (at_bat == 'd') { /* double */ points = 2; } else if (at_bat == 't') { /* triple */ points = 3; } else if (at_bat == 'h') { /* home run */ if (class_standing == 'j') || (class_standing == 's') { points = 4; } else { points = 5; } } else { /* out */ points = 0; }

6 C. Hundhausen, A. O’Fallon 6 You Try It (1) Consider the following scenario: In an effort to make their weather reports more user-friendly, the national weather service would like to translate relative humidity percentages (integers between 0 and 100) into descriptions, as follows: – 20% or lower: "bone dry" – 21% - 40%:"dry" – 41% - 60%:"moderately dry" – 61% - 80%:"moderately humid" – 81% or higher:"humid" Write a nested if statement that, given the relative humidity (an int between 0 and 100), prints out the appropriate description.

7 C. Hundhausen, A. O’Fallon 7 You Try It (2) Solution: int humidity; printf("Please insert the humidty present >> "); scanf("%d",&humidity); if (humidity <= 20) { printf("Bone dry.\n"); } else if (humidity <= 40) { printf("Dry.\n"); } else if (humidity <= 60) { printf("Moderately dry.\n"); } else if (humidity <= 80) { printf("Moderately humid.\n"); } else { /* assume it's between 81 and 100 *`/ printf("Humid.\n"); }

8 C. Hundhausen, A. O’Fallon 8 Nested if statements (3) Nested if statements vs. compound conditionals – Consider the following scenario: The National Weather Service would like to identify hourly weather reports in which the relative humidity is low (below 20%, the temperature is pleasant (between 75 and 85), and the winds are calm (0 to 10 m.p.h.). Assuming that the variables humidity, temp, and wind_speed hold those values, write an if statement that prints out a message when the conditions are met.

9 C. Hundhausen, A. O’Fallon 9 Nested if statements (4) Nested if statements vs. compound conditionals (cont.) – Alternative 1: Nested if if (humidity < 20) if (temp >= 75) if (temp <= 85) if (wind_speed <= 10) printf("Perfect conditions!\n"); – Alternative 2: Compound if conditional if ((humidity = 75) && (temp <= 85) && (wind_speed <= 10)) printf("Perfect conditions!\n");

10 C. Hundhausen, A. O’Fallon 10 Nested if statements (5) Important to note that the C compiler always matches an else with the most recent incomplete if – Example: if (humidity < 20) if (temp <= 32) printf("It's a cool, dry day.") if (wind < 10) printf("Luckily, the winds are calm."); else printf("The humidity is low, it's above freezing."); else if (humidity < 60) if (temp <= 32) printf("It's cold, with moderate humidity."); else printf("It's above freezing, with moderate humidity."); – Do you see a problem here? How would you fix it?

11 C. Hundhausen, A. O’Fallon 11 Nested if statements (6) Guidelines for using nested if statements – Use braces to enclose all if branches, even if they contain only one statement This will obviate the problem of mismatching if and else branches – If possible, structure conditions so each alternative falls on false branch of previous condition (else if…) – In conditionals, don't mistake = for == The C compiler won't be able to catch this error, and you're condition will always evaluate to the same Boolean result!

12 C. Hundhausen, A. O’Fallon 12 Nested if statements (7) Example: Nested if with return statements int get_points(char at_bat) { if (at_bat == 's') /* single */ return 1; /* assertion: at_bat != 's' */ if (at_bat == 'd') /* double */ return 2; /* assertion: at_bat != 's' && at_bat != 'd' */ if (at_bat == 't') /* triple */ return 3; /* assertion: at_bat != 's' && at_bat != 'd' && at_bat != 't' */ if (at_bat == 'h') /* home run */ return 4; /* assertion: at_bat != 's' && at_bat != 'd' && at_bat != 't' && at_bat != 'h' */ return 0; /* out */ }

13 C. Hundhausen, A. O’Fallon 13 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, 2016. P.J. Deitel & H.M. Deitel, C How to Program (7 th Ed.), Pearson Education, Inc., 2013.

14 C. Hundhausen, A. O’Fallon 14 Collaborators Chris Hundhausen


Download ppt "(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University."

Similar presentations


Ads by Google