260 likes | 336 Views
Announcements. I have not graded your exam yet. Multiple Alternative Ifs. if (condition1) statement1; else if (condition2) statement2; … else defaultstatement;. Program. Write a program that displays a letter grade corresponding to an exam score 90 - 100 A 80 - 89 B 70 - 79 C
E N D
Announcements • I have not graded your exam yet CS150 Introduction to Computer Science 1
Multiple Alternative Ifs if (condition1) statement1; else if (condition2) statement2; … else defaultstatement; CS150 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 CS150 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; CS150 Introduction to Computer Science 1
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; What’s the difference? CS150 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: Speed Fee 75 or greater $60.00 51 - 74 $40.00 36 - 50 $20.00 CS150 Introduction to Computer Science 1
Switch Statements • Another form of selection statement • Similar to if’s • Useful for lots of alternatives CS150 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; } CS150 Introduction to Computer Science 1
Form switch (selector) { case label1: statements1; break; case label2: statements2; break; … case labeln: statementsn; break; default: statements; } CS150 Introduction to Computer Science 1
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.”; } Example CS150 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 CS150 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’? CS150 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 CS150 Introduction to Computer Science 1
Questions • Can you write any switch statement as an if? • Can you write any if statement as a switch? CS150 Introduction to Computer Science 1
Problem • Write a switch statement to convert a character digit to an integer CS150 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; CS150 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 CS150 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. CS150 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; CS150 Introduction to Computer Science 1
Prefix and PostfixUnary ++ and -- Prefix Postfix k = --x; k =x--; k = ++x; k = x++; Increment/ Assign value of x to decrement x k, then increment then assign or decrement x value of x to k CS150 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--; CS150 Introduction to Computer Science 1
Program • Write a program that outputs the following: ***** ***** ***** ***** ***** CS150 Introduction to Computer Science 1
Loops Initialize LCV count = 0; while (count < 5) { cout << “*****” << endl; count++; } How many times (iterations) does loop run? Loop Control Variable Change the value of count CS150 Introduction to Computer Science 1
While loops while (logical expression is true) statement; while (logical expression is true) { statement1; statement2; … } CS150 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. CS150 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 CS150 Introduction to Computer Science 1