Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 - Programming for Science Lecture 11: Switch Statements & ?: Operator.

Similar presentations


Presentation on theme: "CSC 107 - Programming for Science Lecture 11: Switch Statements & ?: Operator."— Presentation transcript:

1 CSC 107 - Programming for Science Lecture 11: Switch Statements & ?: Operator

2 Today’s Goal After today, should know ways of writing selections besides if-else statements  When each of the options makes sense  When each selection statements should NOT be used  How to covert between them

3 Choosing From Many Options Consider software controlling playback of voice mail  Action depends on which of 12 buttons is hit  Pushing many keys has same effect  Makes for long, complex if-else statement Imagine choosing from 256 options  Necessary for running Java programs  Code would be very long & hard to read

4 Long if-else statement if (keyHit == 1) { printf(“Please say message to record”); } else if (keyHit == 2) { printf(“Skipping to next message”); } else if (keyHit == 3) { printf(“Playing first new message”); } else if (keyHit == 4) { printf(“Message will be saved for 30 days.”); } else if ((keyHit == 5) || (keyHit == 8)) { printf(“I’m sorry Dave, I can’t do that.”); } else if (keyHit == 6) { printf(“Message deleted”); } else if (keyHit == 7) { printf(“gnidniwer egasseM “); }

5 Better idea: switch Statement switch (keyHit) { case 1: printf(“Please say message to record”); break; case 2: printf(“Skipping to next message”); break; case 3: printf(“Playing first new message”); break; case 4: printf(“Message will be saved for 30 days.”); break; case 5: case 8: printf(“I’m sorry Dave, I can’t do that.”); break; case 6: printf(“Message deleted”); break; case 7: printf(“gnidniwer egasseM “); break; }

6 Outline of switch statement switch (expression) { case label_1: statement;... break; case label_2: case label_3: statement;... break;... default: statement;... break; }

7 switch Statement Selects statements using expression value  Easier to read than if-else Expression evaluated could be literal, variable, or something more complex  Result must be char, short, int, or long Labels must be literal values  E.g., 0, -345, ‘a’, 4351  Can use a symbolic constant (#define) if its value is a literal

8 Executing a switch Statement First evaluates the expression Finds matching case (or default)  Default label is optional  Without default, may not have any match --- program continues execution after switch Execution starts from 1 st matching label  Continues executing until break; found  Forgetting break is a common error -- execution will continue into the next case! Once break hit, continues running from after the switch

9 Tracing switch Statement 0 float temp = -40; 1 float convert; 2 char c = getchar(); 3 switch (toupper(c)) { 4 case ‘C’: 5 convert = (temp / 5) * 9) + 32; 6 break; 7 case ‘F’: 8 convert = ((temp - 32) / 9) * 5; 9 break; 10 default: 11 printf(“Don’t know that scale.”); 12 break; 13 } 14 printf(“Converted temp: %lf\n”, convert); Line #tempconvertc

10 Tracing switch Statement 0 float temp = -40; 1 float convert; 2 char c = getchar(); 3 switch (toupper(c)) { 4 case ‘C’: 5 convert = (temp / 5) * 9) + 32; 6 break; 7 case ‘F’: 8 convert = ((temp - 32) / 9) * 5; 9 break; 10 default: 11 printf(“Don’t know that scale.”); 12 break; 13 } 14 printf(“Converted temp: %lf\n”, convert); Line #tempconvertc

11 Tracing switch Statement 0 float temp = -40; 1 float convert; 2 char c = getchar(); 3 switch (toupper(c)) { 4 case ‘C’: 5 convert = (temp / 5) * 9) + 32; 6 break; 7 case ‘F’: 8 convert = ((temp - 32) / 9) * 5; 9 break; 10 default: 11 printf(“Don’t know that scale.”); 12 break; 13 } 14 printf(“Converted temp: %lf\n”, convert); Line #tempconvertc

12 If-else versus switch switch performs multiple comparisons on single integer expression  Comparisons are for equality only  Labels must be literal values  Actions can overlap by not using break if-else tests many “boolean” expressions  Works with any data types needed  Tests do not have to be related  Actions cannot be overlapped

13 Confession I hate the conditional operator  Makes code hard to read, difficult to debug  Usually used by hackers who think it “cool”  Compiler ultimately converts it to if-else It is important to know, however  Used by hackers who think it “cool”  On rare occasion, can simplify coding  People not required to follow my opinion (yet)

14 Conditional Operator Operator takes three operands 1. “Boolean” expression to evaluate 2. Action to take if expression is “true” 3. Action taken when expression “false” Operator is written like: (expression) ? true_action : false_action

15 Using Conditional Operator Can be used by itself: (a < b) ? foo += 1 : printf(“A is huge”); which is identical to if (a < b) { foo += 1; } else { printf(“A is huge”); }

16 Using Conditional Operator (2) Can also be used within a command: max = (a < b) ? b : a; which would be equivalent to: if (a < b) { max = b; } else { max = a; }

17 Your Turn Get into groups and complete daily activity

18 For Next Lecture Read Sections 3.4 of book  Do not need to understand all the details  But important knowing what is not understood Finish week #4 weekly assignment Finish #3 up and running First programming assignment on web  Due 2 weeks from today


Download ppt "CSC 107 - Programming for Science Lecture 11: Switch Statements & ?: Operator."

Similar presentations


Ads by Google