(4-3) selection structures ii in c h&k chapter 4 instructor - andrew s. o’fallon cpts 121...

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

Upload: maximilian-poole

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

(4-3) Selection Structures II in CH&K Chapter 4

Instructor - Andrew S. O’Fallon

CptS 121 (September 18, 2015)

Washington State University

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

C. Hundhausen, A. O’Fallon2

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.

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

C. Hundhausen, A. O’Fallon3

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; }

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

C. Hundhausen, A. O’Fallon4

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.

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

C. Hundhausen, A. O’Fallon5

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; }

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

C. Hundhausen, A. O’Fallon6

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.

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

C. Hundhausen, A. O’Fallon7

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");

}

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

C. Hundhausen, A. O’Fallon8

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.

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

C. Hundhausen, A. O’Fallon9

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 conditionalif ((humidity < 20) && (temp >= 75) && (temp <= 85) && (wind_speed <= 10))

printf("Perfect conditions!\n");

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

C. Hundhausen, A. O’Fallon10

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?

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

C. Hundhausen, A. O’Fallon11

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!

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

C. Hundhausen, A. O’Fallon12

Nested if statements (7)

Example: Nested if with return statementsint 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 */}

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

C. Hundhausen, A. O’Fallon13

References

J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8th Ed.), Addison-Wesley, 2016.

P.J. Deitel & H.M. Deitel, C How to Program (7th Ed.), Pearson Education , Inc., 2013.

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

C. Hundhausen, A. O’Fallon14

Collaborators

Chris Hundhausen