Download presentation
Presentation is loading. Please wait.
Published byEugene Caldwell Modified over 9 years ago
1
CSC 107 – Programming For Science
2
Today’s Goal Know how to write selections besides if-else When each of the options makes sense When each selection statements should NOT be used How to covert between them
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 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 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) { 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.”; }
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 << “gnidniwer 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
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.”; }
8
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.”; }
9
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.”; }
10
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
Outline of switch statement switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;... }
12
Outline of switch statement 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
13
Outline of switch statement switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;... } Labels must be constant or literal
14
Outline of switch statement 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
15
Outline of switch statement switch (expression) { case label 1 : statement; statement;... break;... case label n : statement; statement; case label n+1 : statement;... break;... default: statement;... } Still legal; execution only stops at break or ending brace
16
Outline of switch statement 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
17
Execution of switch statement 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 ) {
18
Execution of switch statement 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 ) {
19
Execution of switch statement switch (expression) { case label 1 : statement; statement;... break;... case label n : statement; statement; case label n+1 : statement;... break;... default: statement;... } Fall-thru unique to switch; Cannot overlap in if-else if
20
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 (not like if-else s) Execution will only stop at closing brace or break
21
Executing switch Statement 1. Evaluates expression 2. Tries to find 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
22
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;
23
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;
24
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
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
26
Your Turn Get in groups of 3 & work on following activity
27
For Next Lecture Read sections 2.1 – 2.9 for Tuesday How should we write programs? Is there some way to make debugging less painful? Why should we not always rush to the computer? 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. 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.