Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 C. Hundhausen, A. O’Fallon 2 Advantages of switch Statements (1) One issue with nested if statements is readability – The deeper the nesting, the more difficult it can be to figure out what's happening Another issue is that the programmer could mistakenly "mis-nest" if statements, as in the previous example found in lecture set 4-3 In cases in which the nesting is based on the value of a single variable, a switch statement may be a better alternative Offer the potential for faster execution through the program

3 C. Hundhausen, A. O’Fallon 3 Advantages of switch Statements (2) Easier to debug Understandability Maintainability

4 C. Hundhausen, A. O’Fallon 4 switch Statements Example (1) Let's revisit the previous baseball 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.

5 C. Hundhausen, A. O’Fallon 5 switch Statements Example (2) The switch statement provides a cleaner way to handle this scenario: char at_bat; int points; points = 0; printf("Enter the at-bat code (s,d,t,h,o): "); scanf(" %c",&at_bat); switch (at_bat) { case 's': /* single */ points = points + 1; break; case 'd': /* double */ points = points + 2; break; case 't': /* triple */ points = points + 3; break; case 'h': /* home run */ points = points + 4; break; case 'o': points = points + 0; break; default: /* Anything but 's','d','t','h' */ printf("Unrecognized at-bat code."); break; } Don't forget to end each case with break Don't forget the begin and end curly braces If at_bat does not match any other case labels, the default case is executed

6 C. Hundhausen, A. O’Fallon 6 switch Statements Example (3) What if we also want to allow capital letter codes? switch (at_bat) { case 's': /* single */ case 'S': points = points + 1; break; case 'd': /* double */ case 'D': points = points + 2; break; case 't': /* triple */ case 'T': points = points + 3; break; case 'h': /* home run */ case 'H': points = points + 4; break; case 'o': case 'O': points = points + 0; break; default: /* Anything but 's','d','t','h' */ printf("Unrecognized at-bat code."); break; } A single case can contain an arbitrary number of case labels

7 C. Hundhausen, A. O’Fallon 7 switch Statements Example (4) Let's revisit the following 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.

8 C. Hundhausen, A. O’Fallon 8 switch Statements Example (5) We can write this more clearly as a switch statement with embedded if statements : … switch (at_bat) { case 's': /* single */ if (class_standing == 'f') || (class_standing == 'o') { points = 1; } break; case 'd': /* double */ points = 2; break; case 't': /* triple */ points = 3; break; case 'h': /* home run */ if ((class_standing == 'f') || (class_standing == 'o')) { points = 5; } else { points = 4; } break; case 'o': points = 0; break; default: /* Anything but 's','d','t','h' */ printf("Unrecognized at-bat code."); break; }

9 C. Hundhausen, A. O’Fallon 9 You Try It (revisited) 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 switch statement that, given the relative humidity (an int between 0 and 100), prints out the appropriate description. Will this work? Why or why not?

10 C. Hundhausen, A. O’Fallon 10 switch Statements Best Practices Notes on switch statements – You cannot switch based on the value of a double or string variable: char code[20] = "home run"; double era = 1.67; switch (code) { … /* can't do this */ switch (era) { … /* can't do this either */ An arbitrary number of statements can follow a case label It's a good idea always to define a default case A common mistake is to forget to end a case with break. If a break is forgotten, execution "falls" through to the next case label!

11 C. Hundhausen, A. O’Fallon 11 Next Time… Repetition in C – Executing statements while a condition is true – Can eliminate redundant sequential statements

12 C. Hundhausen, A. O’Fallon 12 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.

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


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

Similar presentations


Ads by Google