Skip to main content

Section 4.1 If Statements

Subgoals for Evaluating Selection Statements.

  1. Diagram which statements go together by indentation
  2. For conditional, determine whether expression is true
    1. If true, follow true branch
    1. If false, follow next elif/else branch or exit conditional if no else branch
  3. Repeat step 2 as necessary

Subsection 4.1.1

Given the following declarations:
alpha = 2
beta = 1
delta = 3
eta = 0
gamma = 0
omega = 2.5
theta = -1.3
kappa = 3.0
mu = 0.0
rho = 0.0
Evaluate these statements and determine the value of all variables used.
if kappa > omega:
    rho = kappa + 2

Subsection 4.1.2 1. Diagram which statements go together by indentation

There is only a single indented statement in the body.

Subsection 4.1.3 2. For conditional, determine whether expression is true

We evaluate kappa > omega, substituting 3.0 for kappa and 2.5 for omega.
3.0 > 2.5 is True.

Subsection 4.1.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.
rho = kappa + 2
Substituting 3.0 for kappa, we evaluate the right-hand side to be 5.0 and store that in rho. Therefore, the final value in the variables are:
  • rho: 5.0
  • kappa: 3.0
  • omega: 2.5

Subsection 4.1.5 4. Repeat 2 and 3 as necessary

Repeating is not necessary in this case.
You have attempted of activities on this page.