> num; cout << boolalpha; cout LIMIT ) << endl; return 0; }"> > num; cout << boolalpha; cout LIMIT ) << endl; return 0; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.

Similar presentations


Presentation on theme: "Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume."— Presentation transcript:

1 Computer Science 1620 Selection Structures

2 Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume speed limit = 100 km/h

3 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int num; cout << "Speed: "; cin >> num; cout << boolalpha; cout LIMIT ) << endl; return 0; }

4 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int num; cout << "Speed: "; cin >> num; cout << boolalpha; cout LIMIT ) << endl; return 0; }

5 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << boolalpha; cout LIMIT ) << endl; return 0; }

6 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << boolalpha; cout LIMIT ) << endl; return 0; }

7 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << boolalpha; cout LIMIT ) << endl; return 0; }

8

9 The previous program works Limitations: the program tells us whether the expression is true or false what if we wanted to perform some statement only when a person is speeding: output "Car is speeding" calculate the speeding ticket etc

10 Selection Structures allow the conditional execution of certain statements may or may not be executed two types: if-else statements switch statements

11 If Statement syntax: if ( ) C++ Statement boolean expression this statement will be executed IF AND ONLY IF this expression has value true

12 Example: from the previous program, output "Car is speeding!" if the car is exceeding the speed limit

13 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << boolalpha; cout LIMIT ) << endl; return 0; }

14 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; return 0; } if ( ) C++ Statement boolean expression

15 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; return 0; } if ( ) cout << "Car is speeding!" << endl; boolean expression

16 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; return 0; } if ( ) cout << "Car is speeding!" << endl; speed > LIMIT

17 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; if (speed > LIMIT) cout << "Car is speeding!" << endl; return 0; }

18

19 What statements are legal in an if statement? if ( ) C++ Statement boolean expression Answer: almost anything I/O statements (cin, cout) variable declarations, assignments expressions etc

20 One if – many statements suppose the cost of a speeding ticket is $100 plus an additional $2 for each km over the speed limit adapt our program so that if a car is speeding, it prints "Car is speeding", and prints the price of the speeding ticket in a different statement

21 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; if (speed > LIMIT) cout << "Car is speeding!" << endl; return 0; }

22 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) cout << "Car is speeding!" << endl; cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; return 0; }

23

24

25 One if – many statements when an if statement is used, only the subsequent statement is affected in our example, the speeding ticket calculation was not part of the if statement so it is always executed how do we fix this? Solution 1: use two if statements

26 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) cout << "Car is speeding!" << endl; cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; return 0; }

27 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) cout << "Car is speeding!" << endl; if (speed > LIMIT) cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; return 0; }

28

29

30 One if – many statements the previous solution works Problem: inefficient: repeated code slow: what happens If we have 20 different statements to be conditionally executed

31 Compound Statements syntax: {}{} statement 1; statement 2; statement 3; statement n; … Each statement is a C++ programming statement Executed normally When used with a control structure, it is treated as a single C++ statement

32 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) cout << "Car is speeding!" << endl; if (speed > LIMIT) cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; return 0; }

33 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) { cout << "Car is speeding!" << endl; cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; } return 0; }

34 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) { cout << "Car is speeding!" << endl; cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; } return 0; } The entire compound statement is associated with the if statement.

35 Else consider the program output when a car is not speeding: Nothing to indicate that any computation is taking place

36 Else Solution: When someone is not speeding, output: "Car is not speeding!"

37 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) { cout << "Car is speeding!" << endl; cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; } return 0; }

38 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) { cout << "Car is speeding!" << endl; cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; } if (speed <= LIMIT) cout << "Car is not speeding!" << endl; return 0; }

39

40 Else the previous solution works Problem: inefficient: repeated test Solution: else

41 Else Syntax: if ( ) else C++ Statement boolean expression C++ Statement if expression is true, this statement is executed if expression is false, this statement is executed

42 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) { cout << "Car is speeding!" << endl; cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; } if (speed <= LIMIT) cout << "Car is not speeding!" << endl; return 0; }

43 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) { cout << "Car is speeding!" << endl; cout << "Ticket: $" << 100.00 + 2 * (speed – LIMIT) << endl; } else cout << "Car is not speeding!" << endl; return 0; }

44

45 A quick note about if … else some programmers will use braces with their if statements, even if there is only one statement Programmer preference – and will be treated as such on your assignments/exams if (speed > LIMIT) { cout << "Car is speeding!" << endl; } else { cout << "Car is not speeding!" << endl; }

46 Example: Suppose there are two scholarships available to university students. Scholarship A requires that the student is from Alberta, while scholarship B requires that the student is from Alberta AND has a GPA of 80%. Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship.

47 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; }

48 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; }

49 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; }

50 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; }

51 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; } Scholarship A requires that the student is from Alberta

52 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; } Scholarship A requires that the student is from Alberta

53 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; } Scholarship B requires that the student is from Alberta and has a GPA of 80 or better.

54 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; } Scholarship B requires that the student is from Alberta and has a GPA of 80 or better.

55

56 The following program works, but there is an inefficiency How do we fix this? recall that almost any C++ statement can be included in an if statement the if statement itself is a C++ statement therefore, we can nest one if statement inside the other. if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; We test for albertan being true twice.

57 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; return 0; }

58 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) { cout << "You are eligible for scholarship A." << endl; if (albertan && gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; } return 0; }

59 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) { cout << "You are eligible for scholarship A." << endl; if (gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; } return 0; }

60

61 Nested If Statements when an if statement is used within another if statement, the second if statement is called nested we can nest as many if statements as we like example: Scholarship C requires a student to be from Alberta, have a GPA of 80%, and be a full time student. Incorporate this scholarship information into the previous program.

62 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; if (albertan) { cout << "You are eligible for scholarship A." << endl; if (gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; } return 0; }

63 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; bool fulltime; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; cout << "Full time or part time (0=part, 1=full): "; cin >> fulltime; if (albertan) { cout << "You are eligible for scholarship A." << endl; if (gpa >= 80.0f) cout << "You are eligible for scholarship B." << endl; } return 0; }

64 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether a person is from Alberta, and their GPA, and determines their eligibility for each scholarship. #include using namespace std; int main() { bool albertan; float gpa; bool fulltime; cout << "Are you from Alberta (0=no, 1=yes): "; cin >> albertan; cout << "GPA: "; cin >> gpa; cout << "Full time or part time (0=part, 1=full): "; cin >> fulltime; if (albertan) { cout << "You are eligible for scholarship A." << endl; if (gpa >= 80.0f) { cout << "You are eligible for scholarship B." << endl; if (fulltime) cout << "You are eligible for scholarship C." << endl; } return 0; }

65 Nested If Statement the else clause can be embedded into an if statement as well Example: Suppose you know that a person is either from Alberta or Saskatchewan. Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. NorthSouth Alberta780403 Saskatchewan306

66 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; }

67 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; }

68 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; }

69 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; }

70 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780403 Saskatchewan306

71 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780403 Saskatchewan306

72 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780403 Saskatchewan306

73 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780403 Saskatchewan306

74 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780403 Saskatchewan306

75 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780403 Saskatchewan306

76 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780403 Saskatchewan306

77 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780403 Saskatchewan306

78

79 Nested If nested ifs and elses must be used cautiously Example: Suppose you didn't know the area code for southern Alberta. hence, you choose to omit outputting this from your program NOTE: 780 still only applies to northern residents

80 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780???? Saskatchewan306

81 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 403" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780???? Saskatchewan306

82 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780???? Saskatchewan306

83

84

85

86 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780???? Saskatchewan306

87 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780???? Saskatchewan306 else matches to closest if

88 Nested ifs an else statement will always match to its closest visible if statement an else statement that is outside a compound statement (block) cannot see into that compound statement (block)

89 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) if (north) cout << "Your area code is 780" << endl; else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780???? Saskatchewan306

90 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads whether the person is from Alberta, and whether the person lives in the north or south half of the province. The program then outputs the area code of that person's phone number. #include using namespace std; int main() { bool albertan; bool north; cout << "Which province are you from (0=Saskatchewan, 1=Alberta): "; cin >> albertan; cout << "Where in your province are you from (0=south, 1=north): "; cin >> north; if (albertan) { if (north) cout << "Your area code is 780" << endl; } else cout << "Your area code is 306" << endl; return 0; } NorthSouth Alberta780???? Saskatchewan306

91

92 Why did this work? since second if statement is inside block and else statement is outside of that block, else cannot see the inside if therefore, closest visible if is the first if we will examine scope more closely later on if (albertan) { if (north) cout << "Your area code is 780" << endl; } else cout << "Your area code is 306" << endl; if (albertan) { if (north) cout << "Your area code is 780" << endl; } else cout << "Your area code is 306" << endl;

93 Example: Consider again the amusement park ride. Rewrite the program so that anyone under 32" is told that they are too short, while anyone above 60" is told they are too tall.

94 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } Rewrite using an if statement.

95 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; if ((height >= 32) && (height <= 60)) cout << "You are eligible to ride. " << endl; else cout << "You are not eligible to ride. " << endl; return 0; } Rewrite using an if statement.

96 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; if ((height >= 32) && (height <= 60)) cout << "You are eligible to ride. " << endl; else cout << "You are not eligible to ride. " << endl; return 0; } Rewrite, specifying whether a person is too tall, or too short.

97 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; if (height >= 32) if (height <= 60) cout << "You are eligible to ride. " << endl; else cout << "You are too tall to ride. " << endl; else cout << "You are too short to ride. " << endl; return 0; } Rewrite, specifying whether a person is too tall, or too short.

98

99 Else if when a decision has more than two outcomes (as in the previous example), it is often written using an else if format Syntax: if ( ) else if ( ) else C++ Statement boolean expression 1 executed if expression 1 is true boolean expression 2 C++ Statement boolean expression 3 C++ Statement executed if expression 2 is true executed if expression 3 is true executed if none of the expressions are true

100 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; if (height >= 32) if (height <= 60) cout << "You are eligible to ride. " << endl; else cout << "You are too tall to ride. " << endl; else cout << "You are too short to ride. " << endl; return 0; } Rewrite with an else if format.

101 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; if (height < 32) cout << "You are too short to ride. " << endl; else if (height > 60) cout << "You are too tall to ride. " << endl; else if ( (height >= 32) && (height <= 60)) cout << "You are eligible to ride. " << endl; return 0; }

102

103 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; if (height < 32) cout << "You are too short to ride. " << endl; else if (height > 60) cout << "You are too tall to ride. " << endl; else if ( (height >= 32) && (height <= 60)) cout << "You are eligible to ride. " << endl; return 0; } Do we really need to test this?

104 Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that reads a height (in inches), and computes whether that person is eligible. #include using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; if (height < 32) cout << "You are too short to ride. " << endl; else if (height > 60) cout << "You are too tall to ride. " << endl; else cout << "You are eligible to ride. " << endl; return 0; }

105 Else if – Advantages easier to read and write no need to parse complicated nested if expression no worries about incorrect if/else matching

106 Else if – How it works else-if is not something special in the language the second if is just the statement for the first else the third if is just the statement for the second else etc … if (height < 32) cout << "You are too short to ride. " << endl; else if (height > 60) cout << "You are too tall to ride. " << endl; else cout << "You are eligible to ride. " << endl; if ( ) else C++ Statement boolean expression C++ Statement

107 Example: The monthly income of a computer salesperson is calculated by the following commission schedule: Monthly SalesIncome Greater than $50K$375 + 16% of sales Less than $50K but greater than or equal to $40K$350 + 14% of sales Less than $40K but greater than or equal to $30K$325 + 12% of sales Less than $30K but greater than or equal to $20K$300 + 9% of sales Less than $20K but greater than or equal to $10K$250 + 5% of sales Less than $10K$200 + 3% of sales *From Program Development and Design Using C++, by Gary Bronson

108 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; }

109 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; }

110 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Monthly SalesIncome Greater than $50K$375 + 16% of sales Less than $50K but greater than or equal to $40K$350 + 14% of sales Less than $40K but greater than or equal to $30K$325 + 12% of sales Less than $30K but greater than or equal to $20K$300 + 9% of sales Less than $20K but greater than or equal to $10K$250 + 5% of sales Less than $10K$200 + 3% of sales

111 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Monthly SalesIncome Greater than $50K$375 + 16% of sales Less than $50K but greater than or equal to $40K$350 + 14% of sales Less than $40K but greater than or equal to $30K$325 + 12% of sales Less than $30K but greater than or equal to $20K$300 + 9% of sales Less than $20K but greater than or equal to $10K$250 + 5% of sales Less than $10K$200 + 3% of sales

112 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Monthly SalesIncome Greater than $50K$375 + 16% of sales Less than $50K but greater than or equal to $40K$350 + 14% of sales Less than $40K but greater than or equal to $30K$325 + 12% of sales Less than $30K but greater than or equal to $20K$300 + 9% of sales Less than $20K but greater than or equal to $10K$250 + 5% of sales Less than $10K$200 + 3% of sales

113 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Monthly SalesIncome Greater than $50K$375 + 16% of sales Less than $50K but greater than or equal to $40K$350 + 14% of sales Less than $40K but greater than or equal to $30K$325 + 12% of sales Less than $30K but greater than or equal to $20K$300 + 9% of sales Less than $20K but greater than or equal to $10K$250 + 5% of sales Less than $10K$200 + 3% of sales

114 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; }

115 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; }

116 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } How many comparisons are made if monthlySales is $75000?

117 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; else if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; else if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; else if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; else if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; else if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } How many comparisons are made if monthlySales is $75000?

118 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; else if ((monthlySales = 40000.00)) income = 350.00 + 0.14 * monthlySales; else if ((monthlySales = 30000.00)) income = 325.00 + 0.12 * monthlySales; else if ((monthlySales = 20000.00)) income = 300.00 + 0.09 * monthlySales; else if ((monthlySales = 10000.00)) income = 250.00 + 0.05 * monthlySales; else if (monthlySales < 10000.00) income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Redundant comparisons!

119 Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + 0.16 * monthlySales; else if (monthlySales >= 40000.00) income = 350.00 + 0.14 * monthlySales; else if (monthlySales >= 30000.00) income = 325.00 + 0.12 * monthlySales; else if (monthlySales >= 20000.00) income = 300.00 + 0.09 * monthlySales; else if (monthlySales >= 10000.00) income = 250.00 + 0.05 * monthlySales; else income = 200.00 + 0.03 * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; }

120 Example: Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

121 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

122 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

123 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

124 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

125 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

126 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

127 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

128 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!"

129

130 Switch an alternative to an if … else chain syntax: switch ( ) { integer literal integer expression case : C++ statements break; integer literal case : C++ statements break; default: C++ statements }

131 Switch – How it works switch ( ) { integer literal integer expression case : C++ statements break; integer literal case : C++ statements break; default: C++ statements }

132 Switch – How it works switch ( ) { integer literal integer expression case : C++ statements break; integer literal case : C++ statements break; default: C++ statements } Step 1: Expression is evaluated.

133 Switch – How it works switch ( ) { integer literal integer expression case : C++ statements break; integer literal case : C++ statements break; default: C++ statements } Step 2: Program attempts to find a literal in a case statement that matches the value of the expression.

134 Switch – How it works switch ( ) { integer literal integer expression case : C++ statements break; integer literal case : C++ statements break; default: C++ statements } Step 2a: If program finds a match between expression and literal, then it executes all statements following that case statement until it hits a break statement.

135 Switch – How it works switch ( ) { integer literal integer expression case : C++ statements break; integer literal case : C++ statements break; default: C++ statements } Step 2b: If program finds no match between expression and literal, then it executes all statements following the default statement

136 Example: Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, print "I can only count to three".

137 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, print "I can only count to three".

138 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, print "I can only count to three".

139 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, print "I can only count to three".

140 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, print "I can only count to three".

141 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, print "I can only count to three".

142 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, print "I can only count to three".

143 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, print "I can only count to three".

144

145 Switch Statement the default in a switch is optional like the else in an if statement if no default code is given, then no computation is done in the event of no matching value

146 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, do nothing.

147 #include using namespace std; int main() { int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; } return 0; } Write a program that reads an integer from the user. If the integer is a 1, then print the string "One". If the integer is a 2, then print the string "Two". If the integer is a 3, then print the string "Three". Otherwise, do nothing.

148

149 Switch Statement the expression and literals must be an integer type booleans char short int long each case statement contains only a single literal cannot indicate a range of statements

150 Switch Statement if an if-else chain: 1) has expressions that all use == AND 2) is comparing only integer types that if-else chain can typically be rewritten as a switch statement

151 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; return 0; } Write a program that takes in an integer, an char, and an integer. If the char is a '+', then print the addition of the two integers. If the char is a '-', then print the subtraction of the two integers. If the char is a '*', then print the multiplication of the two integers. If the char is a '/', then print the division of the two integers. If the char is a '%', then print the modulus of the two integers. If the char is none of these, then print "Unknown operation!" Rewrite using a switch statement!

152 Rewrite using a switch statement! if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; Switch Statement switch (op) { case '+' : cout << (left + right) << endl; break; case '-' : cout << (left - right) << endl; break; case '*' : cout << (left * right) << endl; break; case '/' : cout << (left / right) << endl; break; case '%' : cout << (left % right) << endl; break; default: cout << "Unknown operation" << endl; }

153 Rewrite using a switch statement! if (op == '+') cout << (left + right) << endl; else if (op == '-') cout << (left - right) << endl; else if (op == '*') cout << (left * right) << endl; else if (op == '/') cout << (left / right) << endl; else if (op == '%') cout << (left % right) << endl; else cout << "Unknown operation" << endl; Switch Statement switch (op) { case '+' : cout << (left + right) << endl; break; case '-' : cout << (left - right) << endl; break; case '*' : cout << (left * right) << endl; break; case '/' : cout << (left / right) << endl; break; case '%' : cout << (left % right) << endl; break; default: cout << "Unknown operation" << endl; }

154 #include using namespace std; int main() { int left, right; char op; cout << "Please enter your equation: "; cin >> left >> op >> right; cout << left << " " << op << " " << right << " = "; switch (op) { case '+' : cout << (left + right) << endl; break; case '-' : cout << (left - right) << endl; break; case '*' : cout << (left * right) << endl; break; case '/' : cout << (left / right) << endl; break; case '%' : cout << (left % right) << endl; break; default: cout << "Unknown operation" << endl; } return 0; }

155 Switch & Fall-through what happens if the break statements of a switch statement are omitted? int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: cout << "Three" << endl; break; default: cout << "I can only count to three." << endl; }

156

157 Switch & Fall-through when a matching literal is found, program executes all statements following the case statement until it finds a break statement int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; case 2: cout << "Two" << endl; case 3: cout << "Three" << endl; default: cout << "I can only count to three." << endl; } When user entered a 3, all statements following this case were executed.

158 Switch & Fall-through when a matching literal is found, program executes all statements following the case statement until it finds a break statement int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; case 2: cout << "Two" << endl; case 3: cout << "Three" << endl; default: cout << "I can only count to three." << endl; } When user entered a 2, all statements following this case were executed.

159 Switch & Fall-through when a matching literal is found, program executes all statements following the case statement until it finds a break statement int num; cout << "Please enter a number: "; cin >> num; switch (num) { case 1: cout << "One" << endl; case 2: cout << "Two" << endl; case 3: cout << "Three" << endl; default: cout << "I can only count to three." << endl; } When user entered a 1, all statements following this case were executed.

160 Switch & Fall-through when a break statement is omitted, the code for another case statement is executed this is known as fall-through this is often a source of programming error however, it can also be used to the programmer's advantage

161 Write a program that takes in a character, and prints whether or not that character is a vowel. Please input a character: u u is a vowel Please input a character: t t is not a vowel Please input a character: % % is not a vowel

162 #include using namespace std; int main() { char input; cout << "Please input a character: "; cin >> input; switch (input) { case 'a': cout << input << " is a vowel!" << endl; break; case 'A': cout << input << " is a vowel!" << endl; break; case 'e': cout << input << " is a vowel!" << endl; break; case 'E': cout << input << " is a vowel!" << endl; break; Write a program that takes in a character, and prints whether or not that character is a vowel. continued …

163 case 'i': cout << input << " is a vowel!" << endl; break; case 'I': cout << input << " is a vowel!" << endl; break; case 'o': cout << input << " is a vowel!" << endl; break; case 'O': cout << input << " is a vowel!" << endl; break; case 'u': cout << input << " is a vowel!" << endl; break; case 'U': cout << input << " is a vowel!" << endl; break; default: cout << input << " is not a vowel!" << endl; } return 0; } Write a program that takes in a character, and prints whether or not that character is a vowel.

164 #include using namespace std; int main() { char input; cout << "Please input a character: "; cin >> input; switch (input) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << input << " is a vowel!" << endl; break; default: cout << input << " is not a vowel!" << endl; } return 0; } Write a program that takes in a character, and prints whether or not that character is a vowel.

165 #include using namespace std; int main() { char input; cout << "Please input a character: "; cin >> input; switch (input) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': cout << input << " is a vowel!" << endl; break; default: cout << input << " is not a vowel!" << endl; } return 0; } Write a program that takes in a character, and prints whether or not that character is a vowel.


Download ppt "Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume."

Similar presentations


Ads by Google