Chapter 2

Control Flow

Make decisions, repeat work, and handle simple errors.

Boolean expressions

Comparisons produce True or False. Control flow depends on these values, because a program has to know whether it should keep going the same way or choose something else.

At this stage, think of a comparison as a question the program asks itself. The answer is not a sentence, just one of two values, and that small answer decides what happens next.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Type `print(8 > 5)` and run it.
EXERCISE
Output will appear here.
Change `8 > 5` to `8 < 5`.
EXERCISE
Output will appear here.
Predict the result of `print(5 == 5)`.
EXERCISE
Output will appear here.
Print the results of one greater-than, one less-than, and one equals comparison.
EXERCISE
Output will appear here.

If and else

Use a conditional to select one path through a program. This is the point where Python starts making choices instead of simply running top to bottom.

Python checks the condition after if. If it is true, the indented block runs. Otherwise, Python skips it and runs the else block. The indentation is part of the structure, so keep all lines in the same block aligned.

This pattern is useful whenever there are only two realistic outcomes, such as wet or dry, valid or invalid, or warm enough or not warm enough. The main skill here is reading the condition carefully before you run the code.

EXAMPLE
Output will appear here.
Trace the programMove the value, then play through each branch or loop step.
  1. value is below 5
  2. take the else branch

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Type an `if` that prints `yes` when `age` is at least 18 and `no` otherwise.
EXERCISE
Output will appear here.
Set `age` to 17, then to 18. Run after each change.
EXERCISE
Output will appear here.
Write a comment predicting which branch runs for `age = 20`.
EXERCISE
Output will appear here.
Ask whether `temperature` is below 20 and print one clear message for each result.
EXERCISE
Output will appear here.

Elif branches

elif lets you test more than two outcomes. It is the middle ground between a simple yes-or-no choice and a larger decision tree.

When there are several possible answers, think about which one should be checked first. The order matters, because Python stops as soon as it finds a true condition.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Type the three grade branches from the lesson example.
EXERCISE
Output will appear here.
Change `grade` to 95, 82, and 60, one value at a time.
EXERCISE
Output will appear here.
Predict which branch runs for `grade = 90`.
EXERCISE
Output will appear here.
Print `small`, `medium`, or `large` for a number using `if`, `elif`, and `else`.
EXERCISE
Output will appear here.

For loops

A for loop visits every item in a collection. Instead of writing the same line again and again, you let Python move through the sequence for you.

This is one of the most common patterns in programming. It shows up whenever you want to display a list, total a set of numbers, or apply the same rule to each item.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Type `for number in [1, 2, 3]:` and print `number` inside the loop.
EXERCISE
Output will appear here.
Replace the list with `[2, 4, 6]`.
EXERCISE
Output will appear here.
Predict how many lines `range(4)` prints.
EXERCISE
Output will appear here.
Loop through three names and print `Hello,` before each name.
EXERCISE
Output will appear here.

While loops

A while loop continues while its condition is true. It is the right tool when you do not know the number of repetitions in advance, but you do know the condition that should eventually stop the loop.

The line count -= 1 is what moves this loop toward its stopping point. Without a change like that, the condition can stay true forever. Before running a while loop, identify the value that will eventually make its condition false.

That habit protects you from infinite loops and makes the code easier to reason about. If you can point to the value that changes each time, you are already partway to understanding the loop.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Type a loop that starts at `count = 3`, prints it, and subtracts one.
EXERCISE
Output will appear here.
Change the starting count from 3 to 5.
EXERCISE
Output will appear here.
Predict the final number printed before the loop stops.
EXERCISE
Output will appear here.
Count down from 4 and print `done` after the loop.
EXERCISE
Output will appear here.

Break and continue

continue skips one iteration; break finishes a loop early. Both are ways of adjusting the normal flow when a special case appears.

These tools are most useful when a loop has one rule for the common case and a separate rule for an exception. They are powerful, but they should still feel easy to explain in plain language.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Loop through `range(1, 6)` and print each number.
EXERCISE
Output will appear here.
Add `continue` when the number is 3.
EXERCISE
Output will appear here.
Predict which numbers print after adding `break` at 5.
EXERCISE
Output will appear here.
Print numbers 1 through 8 but skip 4 and stop after 6.
EXERCISE
Output will appear here.