0% found this document useful (0 votes)
344 views1 page

Section 3.2 - CS 010A: Introduction To Computer Science For Science, Mathematics & Engineering I

This document discusses if and if-else statements in C++. It provides an example of an if statement that gives a hotel discount if a user's age is over 65. It then has interactive exercises asking the user to determine the value of a variable based on if statements. It defines an if-else statement as executing one group of statements if an expression is true and another group if it is false.

Uploaded by

Claire xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views1 page

Section 3.2 - CS 010A: Introduction To Computer Science For Science, Mathematics & Engineering I

This document discusses if and if-else statements in C++. It provides an example of an if statement that gives a hotel discount if a user's age is over 65. It then has interactive exercises asking the user to determine the value of a variable based on if statements. It defines an if-else statement as executing one group of statements if an expression is true and another group if it is false.

Uploaded by

Claire xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

' My library > CS 010A: Introduction to Computer Science for Science, Mathematics & Engineering I home > 3.

3.2: If-else ( zyBooks catalog ) Help/FAQ * Hayoung Kwon &

!3.1 If-else branches (general)

Students:

"
Section 3.2 is a part of 2 assignments: Week 3 Participation Activities
& Requirements: PA
This assignment's due date has passed. Activity will still be recorded, but will not
count towards this assignment (unless the due date is changed). See this article for Entire class was due: 01/18/2021, 10:00 PM PST
more info.

3.2 If-else

If statements

An if statement executes a group of statements if an expression is true. Braces surround the if branch's statements. Braces { }, sometimes
redundantly called curly braces, represent a grouping, such as a grouping of statements. Note: { } are braces, [ ] are brackets.
Good practice is to indent a branch's statements, using a consistent number of spaces. This material indents 3 spaces.

PARTICIPATION
ACTIVITY
3.2.1: if statement: Hotel discount.

Start 2x speed

#include <iostream>
using namespace std;

int main() { hotelRate = 155;


int hotelRate;
int userAge; cout << "Enter age: ";
cin >> userAge;
hotelRate = 155;
if (userAge > 65) {
cout << "Enter age: ";
cin >> userAge; hotelRate = hotelRate - 20;

if (userAge > 65) { }


hotelRate = hotelRate - 20;
} cout << "Your Hotel rate: ";
cout << hotelRate << endl;
cout << "Your hotel rate: ";
cout << hotelRate << endl;

return 0;
} Memory
95
96 135 hotelRate
Enter age: 68 97 68 userAge
Your hotel rate: 135
98

Captions $
1. An if statement executes a group of statements if an expression is true. The program
assigns hotelRate with 155 and then gets the user's age from input.
2. userAge is 68, so the expression 68 > 65 is true, and the if's statement will execute. Thus, the
statement following the opening brace { will execute next.
3. hotelRate is decreased by 20, which is the discount for guests older than 65.
4. The closing brace } indicates the end of the group of statements.
5. The program completes by printing the hotel rate.

Feedback?

PARTICIPATION
ACTIVITY
3.2.2: If statement.

What is the \nal value of numItems?

1) bonusVal = 19;
numItems = 1;
if (bonusVal > 10) {
numItems = numItems + 3;
}

Check Show answer

2) bonusVal = 0;
numItems = 1;
if (bonusVal > 10) {
numItems = numItems + 3;
}

Check Show answer

Feedback?

If-else statement

An if-else statement executes one group of statements when an expression is true, and another group of statements when the expression
is false.

Construct 3.2.1: If-else statement.

// Statements that execute before the branches

if (expression) {
// Statements that execute when expression is true (first branch)
}
else {
// Statements that execute when expression is false (second branch)
}

// Statements that execute after the branches

Feedback?

In the example below, if a user inputs an age less than 25, the statement insurancePrice = 4800 executes. Otherwise,
insurancePrice = 2200 executes.

PARTICIPATION
ACTIVITY
3.2.3: if-else statement: Car insurance.

Start 2x speed

#include <iostream> Memory


using namespace std;
95
int main() {
int userAge;
96 2200 insurancePrice
int insurancePrice; 97 40 userAge
cout << "Enter age: "; 98
cin >> userAge;

if (userAge < 25) {


insurancePrice = 4800;
Enter age: 22
}
else { Annual price: $4800
insurancePrice = 2200;
}
Enter age: 40
cout << "Annual price: $";
Annual price: $2200
cout << insurancePrice << endl;

return 0;
}

Captions $
1. An if-else statement executes a group of statements if an expression is true, and executes
another group of statements otherwise.
2. userAge is 22, so the expression 22 < 25 is true, and the if's statements will execute.
3. user's age is 40, so the expression 40 < 25 is false, and the else's statements will execute.

Feedback?

Car insurance prices

(Car insurance prices for drivers under 25 are higher because 1 in 6 such drivers are involved in an accident each year, vs. 1 in 15
for older drivers. Source: www.census.gov, 2009).

PARTICIPATION
ACTIVITY
3.2.4: If-else statements.

1) What is the \nal value of numItems?


bonusVal = 5;
if (bonusVal < 12) {
numItems = 100;
}
else {
numItems = 200;
}

Check Show answer

2) What is the \nal value of numItems?


bonusVal = 12;
if (bonusVal < 12) {
numItems = 100;
}
else {
numItems = 200;
}

Check Show answer

3) What is the \nal value of numItems?


bonusVal = 15;
numItems = 44;
if (bonusVal < 12) {
numItems = numItems + 3;
}
else {
numItems = numItems + 6;
}
numItems = numItems + 1;

Check Show answer

4) What is the \nal value of bonusVal?


bonusVal = 11;
if (bonusVal < 12) {
bonusVal = bonusVal + 2;
}
else {
bonusVal = bonusVal + 10;
}

Check Show answer

5) What is the \nal value of bonusVal?


bonusVal = 11;
if (bonusVal < 12) {
bonusVal = bonusVal + 2;
bonusVal = 3 * bonusVal;
}
else {
bonusVal = bonusVal + 10;
}

Check Show answer

Feedback?

PARTICIPATION
ACTIVITY
3.2.5: Writing an if-else statement.

Translate each description to an if-else statement as directly as possible. Use { }. (Not checked, but please indent a branch's
statements some consistent number of spaces, such as 3 spaces).

1) If userAge is greater than 62, assign itemDiscount with 15. Else,


assign itemDiscount with 0.

Check Show answer

2) If numPeople is greater than 10, execute groupSize = 2 * groupSize.


Otherwise, execute groupSize = 3 * groupSize and numPeople =
numPeople - 1.

Check Show answer

3) If numPlayers is greater than 11, execute teamSize = 11. Otherwise,


execute teamSize = numPlayers. Then, no matter the value of
numPlayers, execute teamSize = 2 * teamSize.

Check Show answer

Feedback?

CHALLENGE
ACTIVITY
3.2.1: Enter the output for the if-else branches.

Start
1

Type the program's output


2

#include <iostream>
using namespace std;
3
int main() {
int numApples;

numApples = 4;
d
if (numApples < 2) {
cout << "b" << endl; h
}
else {
cout << "d" << endl;
}

cout << "h" << endl;

return 0;
}

1 2 3

Check Next

Feedback?

CHALLENGE
ACTIVITY
3.2.2: Basic if-else expression.

Start
1

Write an expression that will cause the following code to print "less than 20" if the value of userAge is less than 20.
2
1 #include <iostream>
2 using namespace std;
3 3
4 int main() {
5 int userAge;
6
7 cin >> userAge; // Program will be tested with values: 18, 19, 20, 21.
8
9 if (/* Your code goes here */) {
10 cout << "less than 20" << endl;
11 }
12 else {
13 cout << "20 or more" << endl;
14 }
15
16 return 0;
17 }

1 2 3

Check Next

Feedback?

CHALLENGE
ACTIVITY
3.2.3: Basic if-else.

Start
1

Write an if-else statement for the following:


2

If userTickets is less than 7, execute awardPoints = 1. Else, execute awardPoints = userTickets.

Ex: If userTickets is 3, then awardPoints = 1.


1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 int awardPoints;
6 int userTickets;
7
8 cin >> userTickets; // Program will be tested with values: 5, 6, 7, 8.
9
10 /* Your code goes here */
11
12 cout << awardPoints << endl;
13
14 return 0;
15 }
16

1 2

Check Next

Feedback?

Multi-branch if-else statements

An If-else statement can be extended to have three (or more) branches. Each branch's expression is checked in sequence. As soon as one
branch's expression is found to be true, that branch's statement execute (and no subsequent branch is considered). If no expression is true,
the else branch executes.

Construct 3.2.2: Multi-branch if-else statement. Only 1 branch will execute.


if (expression1) {
// Statements that execute when expression1 is true
// (first branch)
}
else if (expression2) {
// Statements that execute when expression1 is false and expression2 is true
// (second branch)
}
else {
// Statements that execute when expression1 is false and expression2 is false
// (third branch)
}

Feedback?

The equality operator == evaluates to true if the left side and right side are equal. Ex: If numYears holds the value 10, then the expression
numYears == 10 evaluates to true.
Note that the equality operator is ==, not =.

Figure 3.2.1: Multi-branch if-else example: Anniversaries.

#include <iostream>
using namespace std;

int main() {
int numYears;
Enter number years married: 10
cout << "Enter number years married: "; A whole decade -- impressive.
cin >> numYears;
...
if (numYears == 1) {
cout << "Your first year -- great!" << endl; Enter number years married: 25
} Your silver anniversary -- enjoy.
else if (numYears == 10) {
cout << "A whole decade -- impressive." << endl; ...
}
else if (numYears == 25) { Enter number years married: 30
cout << "Your silver anniversary -- enjoy." << endl; Nothing special.
}
else if (numYears == 50) { ...
cout << "Your golden anniversary -- amazing." << endl;
} Enter number years married: 1
else { Your first year -- great!
cout << "Nothing special." << endl;
}

return 0;
}

Feedback?

PARTICIPATION
ACTIVITY
3.2.6: Multi-branch if-else statements.

What is the \nal value of employeeBonus for each given value of numSales?
if (numSales == 0) {
employeeBonus = 0;
}
else if (numSales == 1) {
employeeBonus = 2;
}
else if (numSales == 2) {
employeeBonus = 5;
}
else {
employeeBonus = 10;
}

1) numSales is 2

Check Show answer

2) numSales is 0

Check Show answer

3) numSales is 7

Check Show answer

Feedback?

Common errors

When a branch has a single statement, the braces are optional, but good practice always uses the braces. Always using braces even when
a branch only has one statement prevents the common error of mistakenly thinking a statement is part of a branch.

PARTICIPATION
ACTIVITY
3.2.7: Common error when omitting braces.

Start 2x speed
Memory
if (numSales < 20) 15 < 20 95
salesBonus = 0;
else 96 15 numSales
totBonus = totBonus + 1; 97 20 salesBonus
salesBonus = 20;
98 2 totBonus
Indentation is irrelevant.
salesBonus = 20; is not part of else,
so always executes.
Memory
if (numSales < 20) { 15 < 20 95
salesBonus = 0;
}
96 15 numSales
else { 97 0 salesBonus
totBonus = totBonus + 1;
salesBonus = 20; 98 2 totBonus
}

Always using braces avoids


the above common error.

Captions $
1. Braces aren't used, so the else branch's only statement is totBonus = totBonus + 1. But,
salesBonus = 20; should also be part of the else branch.
2. Always using braces avoids the common error of not including all statements within an if or
else branch.

Feedback?

PARTICIPATION
ACTIVITY
3.2.8: Braces are important.

Omitting braces is a common source of errors. What is the \nal value of numItems?

1) numItems = 0;
bonusVal = 19;
if (bonusVal > 10)
numItems = bonusVal;
numItems = numItems + 1;

Check Show answer

2) numItems = 0;
bonusVal = 5;
if (bonusVal > 10)
// Need to update bonusVal
numItems = bonusVal;
numItems = numItems + 1;

Check Show answer

3) numItems = 0;
bonusVal = 5;
if (bonusVal > 10)
// Update bonusVal
bonusVal = bonusVal - 1;
numItems = bonusVal;
numItems = numItems + 1;

Check Show answer

Feedback?

CHALLENGE
ACTIVITY
3.2.4: If-else statement: Fix errors.

Re-type the code and \x any errors. The code should convert non-positive numbers to 1.
if (userNum > 0)
cout << "Positive." << endl;
else
cout << "Not positive, converting to 1." << endl;
userNum = 1;

cout << "Final: " << userNum << endl;

1 #include <iostream>
2 using namespace std; 1 test
3 passed
4 int main() {
5 int userNum;
6 All tests
7 cin >> userNum; passed
8
9 /* Your solution goes here */
10
11 return 0;
12 }

Run

View your last submission %


Feedback?

How was this section? ! " Provide feedback

Activity summary for assignment: Week 3 Participation Activities & (163 of 173 points)

Was due: 01/18/2021, 10:00 PM PST 163/173 submitted to


This assignment's due date has passed. Activity will still be recorded, but will not count towards this assignment (unless the due date is canvas
changed). See this article for more info.

Completion details %

#3.3 More if-else

You might also like