View
218
Download
0
Tags:
Embed Size (px)
Citation preview
Computer Science 1620
Selection Structures
Write a program that accepts the speed of a vehicle, and determines whether that person is speedingassume speed limit = 100 km/h
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;
}
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;
}
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;
}
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;
}
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;
}
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
Selection Structuresallow the conditional execution of certain
statementsmay or may not be executed
two types: if-else statementsswitch statements
If Statementsyntax:
if ( )
C++ Statement
boolean expression
this statement will be executed IF AND ONLY IF this expression has value true
Example: from the previous program, output "Car is
speeding!" if the car is exceeding the speed limit
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;
}
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
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
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
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;
}
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
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
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;
}
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;
}
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
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;
}
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;}
One if – many statements the previous solution worksProblem:
inefficient: repeated codeslow: what happens If we have 20 different
statements to be conditionally executed
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
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;}
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;
}
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.
Elseconsider the program output when a car is
not speeding:
• Nothing to indicate that any computation is taking place
ElseSolution: When someone is not speeding,
output: "Car is not speeding!"
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;
}
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;
}
Else the previous solution worksProblem:
inefficient: repeated test
Solution: else
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
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;
}
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;
}
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;}
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.
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;
}
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;
}
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;
}
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;
}
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
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
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.
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.
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.
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;
}
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;
}
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;
}
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.
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;
}
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;
}
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;}
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
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;}
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;}
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;}
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;}
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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
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
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;
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.
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.
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.
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.
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.
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
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.
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;
}
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?
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;
}
Else if – Advantageseasier to read and write
no need to parse complicated nested if expression
no worries about incorrect if/else matching
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
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
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;}
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;}
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
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
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
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
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;}
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;}
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?
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?
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!
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;}
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!"
#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!"
#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!"
#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!"
#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!"
#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!"
#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!"
#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!"
#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!"
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
}
Switch – How it works
switch ( ) {
integer literal
integer expression
case :
C++ statements
break;
integer literalcase :
C++ statements
break;default:
C++ statements
}
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.
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.
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.
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
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".
#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".
#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".
#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".
#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".
#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".
#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".
#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".
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
#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.
#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.
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
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
#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!
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;}
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;}
#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;}
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; }
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.
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.
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.
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
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
#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 …
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.
#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.
#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.