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

Slides:



Advertisements
Similar presentations
Chapter 4: Making Decisions.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
Chapter 6 Control Structures.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
Computer Science 1620 Programming & Problem Solving.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
The If/Else Statement, Boolean Flags, and Menus Page 180
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 4 Program Control Statements
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 4 Selection Structures: Making Decisions.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
CONTROLLING PROGRAM FLOW
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Chapter 4 Selection: the if-else and switch-case instructions.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
A First Book of C++ Chapter 4 Selection.
The Ohio State University
Chapter 3 Selection Statements
Chapter 3 Control Statements
CHAPTER 4 Selection CSEG1003 Introduction to Computing
EGR 2261 Unit 4 Control Structures I: Selection
Introduction to C++ October 2, 2017.
Conditionals & Boolean Expressions
Expression Review what is the result and type of these expressions?
Chapter 7 Conditional Statements
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
Presentation transcript:

Computer Science 1620 Selection Structures

Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume 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 using namespace std; int main() { const int LIMIT = 100; int num; cout << "Speed: "; cin >> num; cout << boolalpha; cout 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 using namespace std; int main() { const int LIMIT = 100; int num; cout << "Speed: "; cin >> num; cout << boolalpha; cout 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 using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << boolalpha; cout 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 using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << boolalpha; cout 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 using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << boolalpha; cout LIMIT ) << endl; return 0; }

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

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

If Statement syntax: 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 using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << boolalpha; cout 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 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 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 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 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 statements suppose the cost of a speeding ticket is $100 plus an additional $2 for each km over the speed limit adapt our program so that if a car is speeding, it prints "Car is speeding", and prints the price of the speeding ticket in a different statement

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

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

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

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

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

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

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

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

Else Solution: 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 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: $" << * (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 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: $" << * (speed – LIMIT) << endl; } if (speed <= LIMIT) cout << "Car is not speeding!" << endl; return 0; }

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

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

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years Write a program that accepts the speed of a vehicle, and determines whether that person is speeding. #include using namespace std; int main() { const int LIMIT = 100; int speed; cout << "Speed: "; cin >> speed; cout << setprecision(2) << fixed << showpoint; if (speed > LIMIT) { cout << "Car is speeding!" << endl; cout << "Ticket: $" << * (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 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: $" << * (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 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 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 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 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 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 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 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 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 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 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 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 Statements when an if statement is used within another if statement, the second if statement is called nested we can nest as many if statements as we like example: Scholarship C requires a student to be from Alberta, have a GPA of 80%, and be a full time student. Incorporate this scholarship information into the previous program.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 using namespace std; int main() { int height; cout << "Please enter your height: "; cin >> height; cout << boolalpha; cout = 32) && (height <= 60)) << endl; return 0; } Rewrite using an if statement.

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

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

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

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

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

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

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

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

Else if – Advantages easier 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: The monthly income of a computer salesperson is calculated by the following commission schedule: Monthly SalesIncome Greater than $50K$ % of sales Less than $50K but greater than or equal to $40K$ % of sales Less than $40K but greater than or equal to $30K$ % of sales Less than $30K but greater than or equal to $20K$ % of sales Less than $20K but greater than or equal to $10K$ % of sales Less than $10K$ % 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 using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * 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 using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * 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 using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Monthly SalesIncome Greater than $50K$ % of sales Less than $50K but greater than or equal to $40K$ % of sales Less than $40K but greater than or equal to $30K$ % of sales Less than $30K but greater than or equal to $20K$ % of sales Less than $20K but greater than or equal to $10K$ % of sales Less than $10K$ % of sales

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Monthly SalesIncome Greater than $50K$ % of sales Less than $50K but greater than or equal to $40K$ % of sales Less than $40K but greater than or equal to $30K$ % of sales Less than $30K but greater than or equal to $20K$ % of sales Less than $20K but greater than or equal to $10K$ % of sales Less than $10K$ % of sales

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Monthly SalesIncome Greater than $50K$ % of sales Less than $50K but greater than or equal to $40K$ % of sales Less than $40K but greater than or equal to $30K$ % of sales Less than $30K but greater than or equal to $20K$ % of sales Less than $20K but greater than or equal to $10K$ % of sales Less than $10K$ % of sales

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } Monthly SalesIncome Greater than $50K$ % of sales Less than $50K but greater than or equal to $40K$ % of sales Less than $40K but greater than or equal to $30K$ % of sales Less than $30K but greater than or equal to $20K$ % of sales Less than $20K but greater than or equal to $10K$ % of sales Less than $10K$ % of sales

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * 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 using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * 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 using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if ((monthlySales = )) income = * monthlySales; if (monthlySales < ) income = * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } How many comparisons are made if monthlySales is $75000?

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; else if ((monthlySales = )) income = * monthlySales; else if ((monthlySales = )) income = * monthlySales; else if ((monthlySales = )) income = * monthlySales; else if ((monthlySales = )) income = * monthlySales; else if (monthlySales < ) income = * monthlySales; cout << fixed << showpoint << setprecision(2) << endl; cout << "The income is $" << income << endl; return 0; } How many comparisons are made if monthlySales is $75000?

Write a C++ program that calculates and outputs the monthly income of a computer salesperson based on sales. #include using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; else if ((monthlySales = )) income = * monthlySales; else if ((monthlySales = )) income = * monthlySales; else if ((monthlySales = )) income = * monthlySales; else if ((monthlySales = )) income = * monthlySales; else if (monthlySales < ) income = * 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 using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= ) income = * monthlySales; else if (monthlySales >= ) income = * monthlySales; else if (monthlySales >= ) income = * monthlySales; else if (monthlySales >= ) income = * monthlySales; else if (monthlySales >= ) income = * monthlySales; else income = * 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 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 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 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 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 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 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 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 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 literal case : C++ statements break; default: C++ statements }

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

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

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

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

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

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 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 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 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 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 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 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 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 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 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 == AND 2) is comparing only integer types that if-else chain can typically be rewritten as a switch statement

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

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

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-through when a break statement is omitted, the code for another case statement is executed this is known as fall-through this is often a source of programming error however, it can also be used to the programmer's advantage

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

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

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