Download presentation
Presentation is loading. Please wait.
Published byConrad Young Modified over 9 years ago
1
111/18/2015CS150 Introduction to Computer Science 1 Announcements I have not graded your exam yet
2
211/18/2015CS150 Introduction to Computer Science 1 Multiple Alternative Ifs if (condition1) statement1; else if (condition2) statement2; … else defaultstatement;
3
311/18/2015CS150 Introduction to Computer Science 1 Program Write a program that displays a letter grade corresponding to an exam score 90 - 100 A 80 - 89 B 70 - 79 C 60 - 69 D 0-59 F
4
411/18/2015CS150 Introduction to Computer Science 1 Example if (salary < 0.00) tax = -1; else if (salary < 15000.00) tax = 0.15 * salary; else if (salary < 30000.00) tax = (salary - 15000.00)*0.16 + 2250.00; else tax = salary * 0.26;
5
511/18/2015CS150 Introduction to Computer Science 1 What’s the difference? if (x >= 0) x = x + 1; else if (x >= 1) x = x + 2; if (x >= 0) x = x + 1; if (x >= 1) x = x + 2;
6
611/18/2015CS150 Introduction to Computer Science 1 Selection Structure Review Write a program to solve the following problem: A police department needs to calculate fees for speeding tickets. The fees are as follows: SpeedFee 75 or greater$60.00 51 - 74$40.00 36 - 50$20.00
7
711/18/2015CS150 Introduction to Computer Science 1 Switch Statements Another form of selection statement Similar to if’s Useful for lots of alternatives
8
811/18/2015CS150 Introduction to Computer Science 1 Example switch (watts) { case 25: life = 2500; break; case 40: case 60: life = 1000; break; case 75: case 100: life = 750; break; default: life = 0; }
9
911/18/2015CS150 Introduction to Computer Science 1 Form switch (selector) { case label1: statements1; break; case label2: statements2; break; … case labeln: statementsn; break; default: statements; }
10
1011/18/2015CS150 Introduction to Computer Science 1 Example switch (musical_note) { case ‘c’: cout << “do” << endl; break; case ‘d’: cout << “re” << endl; break; case ‘e’: cout << “mi” << endl; break; case ‘f’: cout << “fa” << endl; break; case ‘g’: cout << “sol” << endl; break; case ‘a’: cout << “la” << endl; break; case ‘b’: cout << “ti” << endl; break; default: cout << “An invalid note was read.”; }
11
1111/18/2015CS150 Introduction to Computer Science 1 Important! Selector must be ordinal type Each possible value is a separate case break stops statements for case, otherwise continue with statements for next case
12
1211/18/2015CS150 Introduction to Computer Science 1 Example switch (color) { case ‘R’: case ‘r’: cout << “red” << endl; case ‘B’: case ‘b’: cout << “blue” << endl; case ‘Y’: case ‘y’: cout << “yellow” << endl; } What happens when color is ‘r’? ‘B’? ‘Y’?
13
1311/18/2015CS150 Introduction to Computer Science 1 Example switch (x > y) { case 1: cout << “x greater” << endl; break; case 0: cout << “y greater or equal” << endl; break; } Write as if statement
14
1411/18/2015CS150 Introduction to Computer Science 1 Questions Can you write any switch statement as an if? Can you write any if statement as a switch?
15
1511/18/2015CS150 Introduction to Computer Science 1 Problem Write a switch statement to convert a character digit to an integer
16
1611/18/2015CS150 Introduction to Computer Science 1 Change to switch if (speed > 35) fee = 20.00; else if (speed > 50) fee = 40.00; else if (speed > 75) fee = 60.00;
17
1711/18/2015CS150 Introduction to Computer Science 1 Examples Write an if statement that prints out the level of schooling. (0, none; 1 through 6, elementary; 7 through 8, middle school; 9 through 12, high school; > 12, college) Write a switch statement to do the same
18
1811/18/2015CS150 Introduction to Computer Science 1 Write a Program Input an integer If the integer is positive, increment a variable poscount by 1. If the integer is negative, increment a variable negcount by 1. If neither, increment zerocount by 1.
19
1911/18/2015CS150 Introduction to Computer Science 1 Unary Operators ++ and -- ++ is increment operator x++; is the same as x = x + 1; -- is decrement operator x--; is the same as x = x - 1;
20
2011/18/2015CS150 Introduction to Computer Science 1 Prefix and Postfix Unary ++ and -- PrefixPostfix k = --x;k =x--; k = ++x;k = x++; Increment/Assign value of x to decrement xk, then increment then assignor decrement x value of x to k
21
2111/18/2015CS150 Introduction to Computer Science 1 Example cout << “Value of i is” << i; cout << “Value of i++ is” << i++; cout << “Value of ++i is” << ++i; cout << “Value of --i is” << --i; cout << “Value of i-- is” << i--;
22
2211/18/2015CS150 Introduction to Computer Science 1 Program Write a program that outputs the following: *****
23
2311/18/2015CS150 Introduction to Computer Science 1 count = 0; while (count < 5) { cout << “ ***** ” << endl; count++; } How many times (iterations) does loop run? Loops Loop Control Variable Initialize LCV Change the value of count
24
2411/18/2015CS150 Introduction to Computer Science 1 While loops while (logical expression is true) statement; while (logical expression is true) { statement1; statement2; … }
25
2511/18/2015CS150 Introduction to Computer Science 1 Key ingredients Initialize MUST initialize loop control variable Test The value of loop control variable is tested during each iteration of loop Update Loop control variable is changed during each loop iteration If any one of these is missing or incorrect, your loop won’t run properly--not at all, too many/few times or infinitely.
26
2611/18/2015CS150 Introduction to Computer Science 1 Examples Write a while loop that outputs each integer from 1 to 5 Write a program that inputs the following data for 5 students: name, id# and grade Write a program that inputs the following data for a user specified number of students: name, id# and grade
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.