Check your understanding of the previous reading: if/else syntax, branching, and tracing a program that skips lines.

Question 1

In the following code, the print statement is ____ the if statement:

if conditional:
    print("The conditional was true")

Select one answer, then check.

  • Not quite. "Before" would mean the line comes above the if, unindented.
  • Correct. The print line is indented, so it is part of the if statement's body.
  • Not quite. "After" would mean the line comes below the if statement, unindented.

Question 2

How many branches does this code have?

if conditional:
    print(5)
print(2)

Select one answer, then check.

  • Not quite. There is at least one if statement, so there is at least one branch point.
  • Not quite. Count how many different routes execution can take through this code, not just the number of if statements.
  • Correct. Either conditional is true (runs print(5) then print(2)) or it is false (runs only print(2)) — two possible routes through the code.
  • Not quite. A single if with no elif/else only ever splits into two routes.

Question 3

How many branches does this code have?

if conditional:
    if second_conditional:
        print(5)
    print(1)
print(2)

Select one answer, then check.

  • Not quite. There are two if statements here, so there is more than one branch point.
  • Not quite. List out the different routes execution could take, depending on conditional and second_conditional.
  • Not quite. There are more than two possible routes once you consider that the inner if only runs when conditional is true.
  • Correct. The three routes are: conditional false (only print(2)); conditional true and second_conditional false (print(1), print(2)); conditional true and second_conditional true (print(5), print(1), print(2)).

Question 4

How many branches does this code have?

if conditional:
    print(5)
else:
    print(2)

Select one answer, then check.

  • Not quite. An if/else always has at least one branch point.
  • Not quite. Count the routes: what happens when conditional is true, and what happens when it is false?
  • Correct. Either conditional is true (runs print(5)) or false (runs print(2)) — exactly one of the two bodies always runs.
  • Not quite. There is no nesting here, so there are only two possible routes.

Question 5

Trace this program line by line. Fill in salary and taxes after each step; put an X (without quotes) if a value is not yet defined.

 1 | salary = 1000
 2 | if salary > 100:
 3 |     taxes = .5
 4 | else:
 5 |     taxes = .1
 6 | salary = taxes * salary
 7 | if salary > 100:
 8 |     if salary < 500:
 9 |         taxes = 0
10 |     salary = taxes * salary

A step can repeat a line's values unchanged when that line is only the if/elif/else keyword itself — the condition is checked, but nothing is assigned.

StepLinesalarytaxes
1 1 1000
2 2 1000
3 1000
4 6
5
6
7

Question 6

What is the value of number_of_users after this program executes?

if False:
    number_of_users = 5
print(number_of_users)

Select one answer, then check.

  • Correct. Since the condition is False, the assignment on line 2 never runs, so number_of_users was never created; print raises a NameError.
  • Not quite. Python has no default value for a variable that was never assigned — it simply does not exist yet.
  • Not quite. Python does not pre-create variables with None; a name that was never assigned does not exist.
  • Not quite. Line 2 is inside the if False: body, so it never runs.