Professional Documents
Culture Documents
gineering I home > 3.1: If-else branches (general) ) zyBooks catalog * Help/FAQ + Hayoung Kwon ,
1.22 LAB: Input and Lab
formatted output: Optional
Right-facing arrow Hidden !2.28 LAB: Musical note frequencies
1.23 LAB: Caffeine levels Lab
Optional
Hidden
Students:
$ Print chapter
"
Section 3.1 is a part of 1 assignment: Week 3 Participation Activities Requirements: PA
This assignment's due date has passed. Activity will still be recorded, but will not
2) Wk2:
Variables/Assignments
% count towards this assignment (unless the due date is changed). See this article for
more info.
Entire class was due: 01/18/2021, 10:00 PM PST
2.12 Characters
2.13 Strings
2.14 Binary
Party of: If party of 1: 1 Counter 1
2.15 Integer overYow
2.16 Numeric data types Else If party of 2: 2
Host
2.17 Unsigned
2.18 Debugging Else: 4 3
2.19 Style guidelines
2.20 C++ example: Salary
calculation with variables
Captions &
2.21 C++ example: Married-couple 1. A restaurant host seats patrons. The host seats a party of 1 at the counter.
names with variables 2. A party of 2 is seated at a small table. Other size parties are seated at a large table.
2.22 LAB: Divide by x Lab 3. The host mentally executes the algorithm: If party of 1, seat at counter; Else If party of 2, seat
Hidden at small table; Else seat at large table.
2.23 LAB: Driving costs Lab
Hidden Feedback?
2.24 LAB: Expression for Lab
calories burned during Hidden
workout PARTICIPATION
ACTIVITY
3.1.2: Branch concept.
2.25 LAB: Using math Lab
functions Hidden
Consider the example above.
2.26 LAB: Phone number Lab
breakdown Hidden
1) A party of 1 is sat at _____ .
the counter
a small table
Feedback?
A branch is a program path taken only if an expression's value is true. Ex: A hotel may discount a price only for people over age 60.
PARTICIPATION
ACTIVITY
3.1.3: A simple branch: Hotel discount.
Start 2x speed
Cost: 155
Input: 45
Put "Cost: " to output
Captions &
1. A branch is a program path taken if an expression is true. This program assigns price = 155,
and gets input age. If age > 60, the branch with price = price - 20 should be taken.
2. When executing, if the input age is 72, then age > 60 is true. The branch is taken, so price =
price - 20 executes. The output is thus 135.
3. But, if the input age were 45, then age > 60 is false. The branch is not taken, so the output is
155.
Feedback?
Such a branch is commonly known as an If branch: A branch taken only if an expression is true.
PARTICIPATION
ACTIVITY
3.1.4: Branches.
Feedback?
The following shows how an if branch can be used to compute an absolute value of a number.
PARTICIPATION
ACTIVITY
3.1.5: Example if branch: Computing absolute value.
Start 2x speed
Output
val = Get next input 9
Input: -9
If val < 0 val = -val
45
Input: 45
Put val to output
Captions &
1. This program outputs the absolute value of the input. After getting input val, if val < 0, the
branch val = -val executes. Finally, val is output.
2. When executing, if the input val is -9, then val < 0 is true. The branch is taken, so val = -val
executes, assigning val with 9. So 9 is output.
3. But, if the input val were 45, then val < 0 is false. The branch is not taken, so the output is 45.
Feedback?
PARTICIPATION
ACTIVITY
3.1.6: Example if branch: Absolute value.
Feedback?
If-else branches
An if-else structure has two branches: The Irst branch is taken if an expression is true, else the other branch is taken.
PARTICIPATION
ACTIVITY
3.1.7: If-else branches.
Start 2x speed
Captions &
1. An if-else has two branches. The if branch is taken if the expression is true. Otherwise, the
else branch is taken.
2. This program should output "Can ride. Done." if the input weight < 150, else should output
"Can't ride. Done."
3. If the input weight is 128, weight < 150 is true, so the if branch executes. "Can ride" is output.
4. But, if the input is 175, weight < 150 is false, so the else branch executes. "Can't ride" is
output.
Feedback?
PARTICIPATION
ACTIVITY
3.1.8: If-else branches.
Feedback?
The following example shows how an if-else can be used to get the maximum of two values.
PARTICIPATION
ACTIVITY
3.1.9: If-else example: Max.
Start 2x speed
Else max = y
7
Input: 3 7
Captions &
1. This program should output the maximum of two input values.
2. If x is 7 and y is 3, x > y is true, so the if branch executes, which assigns max with x, so 7.
3. If the input instead is 3 7, x > y is false, so the else executes. max is assigned with y, which is
7. Thus, whether the input is 7 3 or is 3 7, max gets the larger value.
Feedback?
PARTICIPATION
ACTIVITY
3.1.10: If-else example: Max.
Feedback?
If-elseif-else branches
Commonly a programmer wishes to take one of multiple (three or more) branches. An if-else can be extended to an if-elseif-else structure.
Each branch's expression is checked in sequence; as soon as one branch's expression is found to be true, that branch is taken. If no
expression is found true, execution will reach the else branch, which then executes.
PARTICIPATION
ACTIVITY
3.1.11: If-elseif-else branch.
Start 2x speed
Nothing special
Input: 1997
(more statements)
Captions &
1. This program gets a year, then if 1984 outputs "Orwell!", else if 2001 outputs "Space Odyssey!", else outputs "Nothing special",
thus indicating if a year was featured in a famous book/movie.
2. If year equals 1984, the if branch is taken, outputting "Orwell!".
3. If the input is 2001, year equals 1984 is false. So the next branch is checked: year equals 2001 is true, so that branch is taken,
thus outputting "Space Odyssey!".
4. If year equals anything else, like 1997, the if branch is not taken, and else-if branch is not taken, so the else branch is taken.
"Nothing special" is output.
Feedback?
Note: The else part is optional. If omitted, then if none of the previous expressions are true, no branch executes.
PARTICIPATION
ACTIVITY
3.1.12: If-elseif-else.
Else If x equals 0
Put "Neutral" to output
Else If x equals 1
Put "Agrees" to output
Else
Put "Invalid entry" to output
1) If x is 1, what is output?
Disagrees
Neutral
Agrees
Invalid entry
3) Could the programmer have written the three branches in the order x
equals 1, x equals 0, and x equals -1, and achieved the same results?
No
Yes
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.
#3.2 If-else