165
Computer Science 1620 Selection Structures

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

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

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

Computer Science 1620

Selection Structures

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

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

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

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 <iostream> using namespace std;

int main() {

const int LIMIT = 100;

int num; cout << "Speed: "; cin >> num;

cout << boolalpha; cout << "Car is speeding: " << ( speed > LIMIT ) << endl;

return 0;

}

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

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 <iostream> using namespace std;

int main() {

const int LIMIT = 100;

int num; cout << "Speed: "; cin >> num;

cout << boolalpha; cout << "Car is speeding: " << ( speed > LIMIT ) << endl;

return 0;

}

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

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 <iostream> using namespace std;

int main() {

const int LIMIT = 100;

int speed; cout << "Speed: "; cin >> speed;

cout << boolalpha; cout << "Car is speeding: " << ( speed > LIMIT ) << endl;

return 0;

}

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

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 <iostream> using namespace std;

int main() {

const int LIMIT = 100;

int speed; cout << "Speed: "; cin >> speed;

cout << boolalpha; cout << "Car is speeding: " << ( speed > LIMIT ) << endl;

return 0;

}

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

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 <iostream> using namespace std;

int main() {

const int LIMIT = 100;

int speed; cout << "Speed: "; cin >> speed;

cout << boolalpha; cout << "Car is speeding: " << ( speed > LIMIT ) << endl;

return 0;

}

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

The previous program worksLimitations:

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

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

Selection Structuresallow the conditional execution of certain

statementsmay or may not be executed

two types: if-else statementsswitch statements

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

If Statementsyntax:

if ( )

C++ Statement

boolean expression

this statement will be executed IF AND ONLY IF this expression has value true

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

Example: from the previous program, output "Car is

speeding!" if the car is exceeding the speed limit

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

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 <iostream> using namespace std;

int main() {

const int LIMIT = 100;

int speed; cout << "Speed: "; cin >> speed;

cout << boolalpha; cout << "Car is speeding: " << ( speed > LIMIT ) << endl;

return 0;

}

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

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 <iostream> using namespace std;

int main() {

const int LIMIT = 100;

int speed; cout << "Speed: "; cin >> speed;

return 0;

}

if ( )

C++ Statement

boolean expression

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

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 <iostream> 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

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

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 <iostream> 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

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

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 <iostream> 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;

}

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

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

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

One if – many statementssuppose 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

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

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 <iostream> 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;

}

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

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 <iostream>#include <iomanip> 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;

}

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

One if – many statementswhen an if statement is used, only the

subsequent statement is affected in our example, the speeding ticket

calculation was not part of the if statementso it is always executed

how do we fix this?Solution 1: use two if statements

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

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 <iostream>#include <iomanip> 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;

}

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

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 <iostream>#include <iomanip> 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;}

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

One if – many statements the previous solution worksProblem:

inefficient: repeated codeslow: what happens If we have 20 different

statements to be conditionally executed

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

Compound Statementssyntax:

{

}

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

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

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 <iostream>#include <iomanip> 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;}

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

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 <iostream>#include <iomanip> 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;

}

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

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 <iostream>#include <iomanip> 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 associatedwith the if statement.

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

Elseconsider the program output when a car is

not speeding:

• Nothing to indicate that any computation is taking place

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

ElseSolution: When someone is not speeding,

output: "Car is not speeding!"

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

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 <iostream>#include <iomanip> 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;

}

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

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 <iostream>#include <iomanip> 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;

}

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

Else the previous solution worksProblem:

inefficient: repeated test

Solution: else

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

ElseSyntax:

if ( )

else

C++ Statement

boolean expression

C++ Statement

if expression is true, this statement is executed

if expression is false, this statement is executed

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

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 <iostream>#include <iomanip> 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;

}

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

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 <iostream>#include <iomanip> 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;

}

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

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;}

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

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.

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

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 <iostream> 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;

}

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

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 <iostream> 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;

}

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

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 <iostream> 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;

}

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

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 <iostream> 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;

}

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

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 <iostream> 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

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

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 <iostream> 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

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

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 <iostream> 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.

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

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 <iostream> 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.

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

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.

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

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 <iostream> 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;

}

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

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 <iostream> 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;

}

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

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 <iostream> 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;

}

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

Nested If Statementswhen 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.

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

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 <iostream> 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;

}

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

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 <iostream> 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;

}

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

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 <iostream> 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;}

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

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.

North South

Alberta 780 403

Saskatchewan 306 306

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

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 <iostream> 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;}

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

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 <iostream> 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;}

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

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 <iostream> 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;}

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

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 <iostream> 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;}

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

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 <iostream> 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;}

North South

Alberta 780 403

Saskatchewan 306 306

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

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 <iostream> 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;}

North South

Alberta 780 403

Saskatchewan 306 306

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

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 <iostream> 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;}

North South

Alberta 780 403

Saskatchewan 306 306

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

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 <iostream> 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;}

North South

Alberta 780 403

Saskatchewan 306 306

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

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 <iostream> 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;}

North South

Alberta 780 403

Saskatchewan 306 306

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

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 <iostream> 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;}

North South

Alberta 780 403

Saskatchewan 306 306

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

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 <iostream> 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;}

North South

Alberta 780 403

Saskatchewan 306 306

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

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 <iostream> 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;}

North South

Alberta 780 403

Saskatchewan 306 306

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

Nested Ifnested 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

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

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 <iostream> 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;}

North South

Alberta 780 ????

Saskatchewan 306 306

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

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 <iostream> 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;}

North South

Alberta 780 ????

Saskatchewan 306 306

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

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 <iostream> 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;

}

North South

Alberta 780 ????

Saskatchewan 306 306

Page 83: Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume
Page 84: Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume
Page 85: Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume
Page 86: Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume

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 <iostream> 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;

}

North South

Alberta 780 ????

Saskatchewan 306 306

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

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 <iostream> 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;

}

North South

Alberta 780 ????

Saskatchewan 306 306

else matches to closest if

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

Nested ifsan else statement will always match to its

closest visible if statementan else statement that is outside a

compound statement (block) cannot see into that compound statement (block)

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

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 <iostream> 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;

}

North South

Alberta 780 ????

Saskatchewan 306 306

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

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 <iostream> 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;

}

North South

Alberta 780 ????

Saskatchewan 306 306

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

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;

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

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.

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

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 <iostream> using namespace std;

int main() {

int height; cout << "Please enter your height: "; cin >> height;

cout << boolalpha;

cout << "This person is eligible to ride: " << ((height >= 32) && (height <= 60)) << endl;

return 0;

}Rewrite using an if statement.

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

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 <iostream> 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.

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

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 <iostream> 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.

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

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 <iostream> 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.

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

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 if ( )

else

C++ Statement

boolean expression 1

executed if expression 1 is true

boolean expression 2

C++ Statement

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

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

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 <iostream> 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.

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

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 <iostream>

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;

}

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

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 <iostream>

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?

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

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 <iostream>

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;

}

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

Else if – Advantageseasier to read and write

no need to parse complicated nested if expression

no worries about incorrect if/else matching

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

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

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

Example:

Example:The monthly income of a computer

salesperson is calculated by the following commission schedule:

Monthly Sales Income

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

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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;}

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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;}

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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 Sales Income

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

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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 Sales Income

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

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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 Sales Income

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

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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 Sales Income

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

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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;}

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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;}

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

if ((monthlySales < 20000.00) && (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 comparisonsare made if monthlySalesis $75000?

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

else if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

else if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

else if ((monthlySales < 20000.00) && (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 comparisonsare made if monthlySalesis $75000?

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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 < 50000.00) && (monthlySales >= 40000.00)) income = 350.00 + 0.14 * monthlySales;

else if ((monthlySales < 40000.00) && (monthlySales >= 30000.00)) income = 325.00 + 0.12 * monthlySales;

else if ((monthlySales < 30000.00) && (monthlySales >= 20000.00)) income = 300.00 + 0.09 * monthlySales;

else if ((monthlySales < 20000.00) && (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!

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

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales.

#include <iostream>#include <iomanip> 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;}

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

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!"

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

#include <iostream> 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!"

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

#include <iostream> 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!"

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

#include <iostream> 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!"

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

#include <iostream> 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!"

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

#include <iostream> 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!"

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

#include <iostream> 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!"

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

#include <iostream> 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!"

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

#include <iostream> 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!"

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

Switch an alternative to an if … else chain syntax:

switch ( ) {

integer literal

integer expression

case :

C++ statements

break;

integer literalcase :

C++ statements

break;default:

C++ statements

}

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

Switch – How it works

switch ( ) {

integer literal

integer expression

case :

C++ statements

break;

integer literalcase :

C++ statements

break;default:

C++ statements

}

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

Switch – How it works

switch ( ) {

integer literal

integer expression

case :

C++ statements

break;

integer literalcase :

C++ statements

break;default:

C++ statements

}

Step 1: Expression is evaluated.

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

Switch – How it works

switch ( ) {

integer literal

integer expression

case :

C++ statements

break;

integer literalcase :

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.

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

Switch – How it works

switch ( ) {

integer literal

integer expression

case :

C++ statements

break;

integer literalcase :

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.

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

Switch – How it works

switch ( ) {

integer literal

integer expression

case :

C++ statements

break;

integer literalcase :

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

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

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".

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

#include <iostream> 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".

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

#include <iostream> 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".

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

#include <iostream> 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".

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

#include <iostream> 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".

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

#include <iostream> 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".

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

#include <iostream> 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".

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

#include <iostream> 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".

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

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

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

#include <iostream> 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.

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

#include <iostream> 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.

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

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

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

Switch Statement if an if-else chain:

1) has expressions that all use == AND2) is comparing only integer types

that if-else chain can typically be rewritten as a switch statement

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

#include <iostream> 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!

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

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;}

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

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;}

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

#include <iostream> 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;}

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

Switch & Fall-throughwhat 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; }

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

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.

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

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.

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

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.

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

Switch & Fall-throughwhen 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 errorhowever, it can also be used to the

programmer's advantage

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

Write a program that takes in a character, and prints whether or not that character is a vowel.

Please input a character: uu is a vowel

Please input a character: tt is not a vowel

Please input a character: %% is not a vowel

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

#include <iostream> 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 …

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

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.

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

#include <iostream> 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.

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

#include <iostream> 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.