Section 4.11 Nested 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.11.1
Given the following declarations:
alpha = 2
delta = 3
eta = 0
gamma = 0
omega = 2.5
theta = -1.3
kappa = 3.0
rho = 0.0
Evaluate these statements and determine the value of all variables used.
if omega < kappa:
if alpha < delta:
eta = 5
if alpha < eta:
gamma = 4
rho = -1.0
Subsection 4.11.2 SG1: Diagram which statements go together.
In this diagram, the first thing to note is the parent/outer if-statement highlighted in blue.
Inside the true branch of the parent/outer if-statement, there are two sequential if-statements.
The final single statement is highlighted in green, and it is not part of the nested if-statements, so it will always be executed.

Subsection 4.11.3 SG2: For if statement, determine whether true or false
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
(omega < kappa)
:Subsection 4.11.4 SG3: If true, follow true branch. If false, follow else branch (OR do nothing if there is no else branch).
The true branch contains two sequential if-statements, so we must repeat SG2 and SG3 for each of them.

Subsection 4.11.5 SG2: For if statement, determine whether true or false
Start with the first if-statement in the inner sequence.
First we evaluate
(alpha < delta)
:Subsection 4.11.6 SG3: If true, follow true branch. If false, follow else branch (OR do nothing if there is no else branch).
The condition is TRUE so we follow the true branch.
eta = 5
Subsection 4.11.7 SG2: For if statement, determine whether true or false
Now we do the second if-statement in the inner sequence.
First we evaluate
(alpha < eta)
:Subsection 4.11.8 SG3: If true, follow true branch. If false, follow else branch (OR do nothing if there is no else branch).
The condition is TRUE so we follow the true branch.
gamma = 4
Next, the final sequential statement after the entire selection structure is always executed:
rho = -1.0
Answer.
omega
= 2.5, kappa
= 3.0, alpha
= 2, delta
= 3, eta
= 5, gamma
= 4, rho
= -1.0Practice Pages.
You have attempted 1 of 1 activities on this page.