Section 4.7 If Statements
Subgoals for Evaluating Selection Statements.
- Diagram which statements go together by indentation
-
For conditional, determine whether expression is true
- If true, follow true branch
- If false, follow next elif/else branch or exit conditional if no else branch
- Repeat step 2 as necessary
Subsection 4.7.1
Given the following declarations:
alpha = 2
beta = 1
delta = 3
eta = 0
gamma = 0
Evaluate these statements and determine the value of all variables used.
if alpha > beta:
eta = alpha + 2
if alpha > delta:
gamma = alpha + 5
Subsection 4.7.2 1. Diagram which statements go together by indentation
There is a single indented statement in the body of the first
if
statement, and a single indented statement in the body of the second if
statement.Subsection 4.7.3 2. For conditional, determine whether expression is true
Because there are 2 sequential if-statements, we start with the first one, and then repeat SG2 and SG3 for the other.
First we evaluate alpha > beta:
2 > 1
is True
.Subsection 4.7.4 3. If true, follow true branch; If false, follow next elif/else branch or exit conditional if no else branch
The condition is
True
, so we execute the true branch.eta = alpha + 2
= 2 + 2
= 4
Subsection 4.7.5 SG2: For if statement, determine whether expression is true
Because there are 2 sequential if-statements, we need to repeat SG2 and SG3 for the second if-statement in the sequence.
First we evaluate alpha > delta:
2 > 3
is False
Subsection 4.7.6 SG2.2: If false, follow next elif/else branch or exit conditional if no else branch
The condition is FALSE and there is no else branch, so we do nothing.
Answer.
alpha
= 2, beta
= 1, delta
= 3, eta
= 4, gamma
= 0Practice Pages.
[cross-reference to target(s) "evaluate_conditional-we3-p1" missing or not unique]
You have attempted of activities on this page.