Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHY 107 – Programming For Science. Today’s Goal  Know how to write decisions besides if-else select  Why we use select or if - else and how to choose.

Similar presentations


Presentation on theme: "PHY 107 – Programming For Science. Today’s Goal  Know how to write decisions besides if-else select  Why we use select or if - else and how to choose."— Presentation transcript:

1 PHY 107 – Programming For Science

2 Today’s Goal  Know how to write decisions besides if-else select  Why we use select or if - else and how to choose  Understand times when select statements illegal  How to convert between the two types of statements

3 Press “*” For Horrible Music  Writing software for voicemail system  Which of 12 buttons hit determine action to take  For much of time, many keys have same effect ifelse ifelse  Long, ugly if - else if - else statement results  Now imagine choosing from 256 options  Required for web browsers & other systems  Takes forever to write & code impossible to read

4 Press “*” For Horrible Music  Writing software for voicemail system  Which of 12 buttons hit determine action to take  For much of time, many keys have same effect ifelse ifelse  Long, ugly if - else if - else statement results  Now imagine choosing from 256 options  Required for web browsers & other systems  Takes forever to write & code impossible to read

5 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 is saved for 30 days.”); } else if (keyHit == 6) { printf(“Message deleted”); } else if (keyHit == 7) { printf(“dniwer egasseM”); } else if ((keyHit == 5) || (keyHit == 8)) { printf(“I’m sorry Dave, I can’t do that.”); } else { printf(“Fooled you! Can’t talk to a person.”); }

6 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 is saved for 30 days.”); } else if (keyHit == 6) { printf(“Message deleted”); } else if (keyHit == 7) { printf(“dniwer egasseM”); } else if ((keyHit == 5) || (keyHit == 8)) { printf(“I’m sorry Dave, I can’t do that.”); } else { printf(“Fooled you! Can’t talk to a person.”); }

7 Better idea: 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 is saved for 30 days.”); } else if (keyHit == 6) { printf(“Message deleted”); } else if (keyHit == 7) { printf(“dniwer egasseM”); } else if ((keyHit == 5) || (keyHit == 8)) { printf(“I’m sorry Dave, I can’t do that.”); } else { printf(“Fooled you! Can’t talk to a person.”); }

8 switch 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 6: printf(“Message deleted”; break; case 7: printf(“gnidniwer egasseM”; break; case 5: case 8: printf(“I’m sorry Dave, I can’t do that.”; break; default: printf(“The hit squad will find you soon.”; }

9 switch 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 6: printf(“Message deleted”); break; case 7: printf(“dniwer egasseM”); break; case 5: case 8: printf(“I’m sorry Dave, I can’t do that.”); break; default: printf(“Fooled you! Can’t talk to a person.”); }

10 switch 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 6: printf(“Message deleted”); break; case 7: printf(“dniwer egasseM”); break; case 5: case 8: printf(“I’m sorry Dave, I can’t do that.”); break; default: printf(“Fooled you! Can’t talk to a person.”); }

11 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...}

12 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Anything of ordinal (whole number) type: intdouble charfloat longMonkey intdouble charfloat longMonkey

13 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Anything of ordinal (whole number) type: intdouble charfloat longMonkey intdouble charfloat longMonkey

14 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Anything of ordinal (whole number) type: intdouble charfloat longMonkey intdouble charfloat longMonkey

15 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Labels must be constants or literal

16 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} This is legal; execution only stops at break or ending brace

17 switch Outline of switch statement switch { case break case switch (expression) { case label 1 : statement; statement;... break;... case label n : case break default statement; statement; case label n+1 : statement;... break;... default: statement;...}

18 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Optional, like else must be at end & matches anything left

19 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Same as equality test in if : if (expression == label 1 ) {

20 switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Works like OR (||) in if-else if : else if (expression == label n || expression == label n+1 ) {

21 switch Outline of switch statement switch { case break case switch (expression) { case label 1 : statement; statement;... break;... case label n : case break default statement; statement; case label n+1 : statement;... break;... default: statement;...} Fall-thru unique to switch ; Cannot overlap in if-else if

22 switch switch Statement  Depends on a value to determine what is run  Easier to read than if - else if  Expression can be anything you want  bool, char, short, int, or long result required  Where execution starts marked by the labels  Literals or constants only usable for the labels  Labels in any order: starts at first equal to value  Doesn’t stop at next label (unlike if - else )  Continues executing to closing brace or break  No reevaluation of expression in switch statement

23 switch switch Statement  Depends on a value to determine what is run  Easier to read than if - else if  Expression can be anything you want  bool, char, short, int, or long result required  Where execution starts marked by the labels  Literals or constants only usable for the labels  Labels in any order: starts at first equal to value  Doesn’t stop at next label  Continues executing to closing brace or break  Do not reevaluate expression  Do not reevaluate expression in switch

24 switch Executing switch Statement 1. Evaluates expression 2. Finds first matching case or default  If no default no match, will skip past switch 3. Execution starts at 1 st matching label  Execution will continue until break found  Continues into next case if break is not hit 4. Restarts running after switch once break hit  Legal to reach end of switch without a break  Continues running code after switch

25 switch Tracing switch Statement float temp = -40; // Note: -40 Celsius == -40 Fahrenheit float convert; char c = ‘c’; switch (toupper(c)) { case ‘K’: temp = temp – 273.15; case ‘C’: convert = ((temp / 5) * 9) + 32; break; case ‘F’: convert = ((temp - 32) / 9) * 5; break; default: printf(“Amoronsayswhat?\n”); } printf(“Converted temp: %lf\n”, convert);

26 switch Tracing switch Statement float temp = -40; // Note: -40 Celsius == -40 Fahrenheit float convert; char c = ‘F’; switch (toupper(c)) { case ‘K’: temp = temp – 273.15; case ‘C’: convert = ((temp / 5) * 9) + 32; break; case ‘F’: convert = ((temp - 32) / 9) * 5; break; default: printf(“Amoronsayswhat?\n”); } printf(“Converted temp: %lf\n”, convert);

27 Steel Cage Match if - else switch  Actions cannot overlap  Expression per if, else if  Any boolean expressions  else gets remaining cases only when expression(s) are true  Use for actions needed only when expression(s) are true  Overlap by omitting break;  Evaluates single expression  Only 1 equality test  default gets all other cases when expression has specific value actions should overlap  Use for actions needed when expression has specific value –or – actions should overlap

28 Your Turn  Get in groups & work on following activity

29 For Next Lecture  Week #4 weekly assignment available on D2L  If problem takes more than 10 minutes, TALK TO ME!  Programming Project #1 available on D2L also  One of term’s 3 large assignments – due Oct. 12  Midterm #1 in class on Friday  Wednesday’s lecture give you chance to review


Download ppt "PHY 107 – Programming For Science. Today’s Goal  Know how to write decisions besides if-else select  Why we use select or if - else and how to choose."

Similar presentations


Ads by Google