Section 3.2 - CS 010A: Introduction To Computer Science For Science, Mathematics & Engineering I
Section 3.2 - CS 010A: Introduction To Computer Science For Science, Mathematics & Engineering I
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;
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.
1) bonusVal = 19;
numItems = 1;
if (bonusVal > 10) {
numItems = numItems + 3;
}
2) bonusVal = 0;
numItems = 1;
if (bonusVal > 10) {
numItems = numItems + 3;
}
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.
if (expression) {
// Statements that execute when expression is true (first branch)
}
else {
// Statements that execute when expression is false (second branch)
}
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
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 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.
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).
Feedback?
CHALLENGE
ACTIVITY
3.2.1: Enter the output for the if-else branches.
Start
1
#include <iostream>
using namespace std;
3
int main() {
int numApples;
numApples = 4;
d
if (numApples < 2) {
cout << "b" << endl; h
}
else {
cout << "d" << 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
1 2
Check Next
Feedback?
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.
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 =.
#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
2) numSales is 0
3) numSales is 7
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
}
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;
2) numItems = 0;
bonusVal = 5;
if (bonusVal > 10)
// Need to update bonusVal
numItems = bonusVal;
numItems = numItems + 1;
3) numItems = 0;
bonusVal = 5;
if (bonusVal > 10)
// Update bonusVal
bonusVal = bonusVal - 1;
numItems = bonusVal;
numItems = numItems + 1;
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;
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
Activity summary for assignment: Week 3 Participation Activities & (163 of 173 points)
Completion details %