Download presentation
Presentation is loading. Please wait.
Published byJeffery Ryan Modified over 9 years ago
1
CSC 107 – Programming For Science
2
Announcement
3
Today’s Goal Know how to write selections besides if-else Why we use select or if - else and how to choose Understand times when selection statements illegal How to convert between the two types of statements
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
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
6
Long if-else statement if (keyHit == 1) { cout << “Please say message to record”; } else if (keyHit == 2) { cout << “Skipping to next message”; } else if (keyHit == 3) { cout << “Playing first new message”; } else if (keyHit == 4) { cout << “Message is saved for 30 days.”; } else if (keyHit == 6) { cout << “Message deleted”; } else if (keyHit == 7) { cout << “dniwer egasseM “; } else if ((keyHit == 5) || (keyHit == 8)) { cout << “I’m sorry Dave, I can’t do that.”; } else { cout << “The hit squad will find you soon.”; }
7
Long if-else statement if (keyHit == 1) { cout << “Please say message to record”; } else if (keyHit == 2) { cout << “Skipping to next message”; } else if (keyHit == 3) { cout << “Playing first new message”; } else if (keyHit == 4) { cout << “Message is saved for 30 days.”; } else if (keyHit == 6) { cout << “Message deleted”; } else if (keyHit == 7) { cout << “dniwer egasseM “; } else if ((keyHit == 5) || (keyHit == 8)) { cout << “I’m sorry Dave, I can’t do that.”; } else { cout << “The hit squad will find you soon.”; }
8
Better idea: if (keyHit == 1) { cout << “Please say message to record”; } else if (keyHit == 2) { cout << “Skipping to next message”; } else if (keyHit == 3) { cout << “Playing first new message”; } else if (keyHit == 4) { cout << “Message is saved for 30 days.”; } else if (keyHit == 6) { cout << “Message deleted”; } else if (keyHit == 7) { cout << “dniwer egasseM “; } else if ((keyHit == 5) || (keyHit == 8)) { cout << “I’m sorry Dave, I can’t do that.”; } else { cout << “The hit squad will find you soon.”; }
9
switch Better idea: switch Statement switch (keyHit) { case 1: cout << “Please say message to record”; break; case 2: cout << “Skipping to next message”; break; case 3: cout << “Playing first new message”; break; case 4: cout << “Message will be saved for 30 days.”; break; case 6: cout << “Message deleted”; break; case 7: cout << “gnidniwer egasseM”; break; case 5: case 8: cout << “I’m sorry Dave, I can’t do that.”; break; default: cout << “The hit squad will find you soon.”; }
10
switch Better idea: switch Statement switch (keyHit) { case 1: cout << “Please say message to record”; break; case 2: cout << “Skipping to next message”; break; case 3: cout << “Playing first new message”; break; case 4: cout << “Message will be saved for 30 days.”; break; case 6: cout << “Message deleted”; break; case 7: cout << “dniwer egasseM”; break; case 5: case 8: cout << “I’m sorry Dave, I can’t do that.”; break; default: cout << “The hit squad will find you soon.”; }
11
switch Better idea: switch Statement switch (keyHit) { case 1: cout << “Please say message to record”; break; case 2: cout << “Skipping to next message”; break; case 3: cout << “Playing first new message”; break; case 4: cout << “Message will be saved for 30 days.”; break; case 6: cout << “Message deleted”; break; case 7: cout << “dniwer egasseM”; break; case 5: case 8: cout << “I’m sorry Dave, I can’t do that.”; break; default: cout << “The hit squad will find you soon.”; }
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;...}
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 ) Execution continues until closing brace or break No reevaluation of expression in switch statement
23
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
24
switch Tracing switch Statement float temp = -40; float convert; char c; cin >> 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: cout << “Amoronsayswhat?” << endl; } cout <<“Converted temp: ” << convert << endl;
25
switch Tracing switch Statement float temp = -40; float convert; char c; cin >> 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: cout << “Amoronsayswhat?” << endl; } cout <<“Converted temp: ” << convert << endl;
26
switch Tracing switch Statement float temp = -40; float convert; char c; cin >> 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: cout << “Amoronsayswhat?” << endl; } cout <<“Converted temp: ” << convert << endl;
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 of 3 & work on following activity
29
For Next Lecture Read sections 8.1 – 8.3 for Monday What if we want to do something repeatedly? How should we tell the machine to stop? What is difference between while and do-while ? Week #4 weekly assignment available on Angel If problem takes more than 10 minutes, TALK TO ME! Programming Project #1 available on Angel also One of term’s 3 large assignments – due Oct. 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.