3. Control Flow#
Control flow lets a program decide what to do next. This chapter introduces the branching and looping structures that turn straight-line code into programs that respond to data.
Conditional statements with
if,elif, andelseforloops for iterating over datawhileloops for condition-based repetitionLoop-control keywords for special cases
Chapter Video
This overview introduces Boolean expressions and conditional branches with if, elif, and else.
Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements (16:28) by Corey Schafer
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Use conditional statements to choose between branches of code
Write
forandwhileloops for repeated workUse
break,continue, andpassintentionallyTrace how control moves through nested branches and loops
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Control flow |
The order in which a program runs statements. |
2 |
Conditional statement |
A statement that chooses whether to run code based on a condition. |
3 |
Boolean expression |
An expression that evaluates to |
4 |
Branch |
One possible path through a conditional statement. |
5 |
|
A conditional statement that runs a block only when its condition is true. |
6 |
|
The branch that runs when the |
7 |
|
A chained conditional branch that tests another condition. |
8 |
Nested conditional |
A conditional statement placed inside another conditional statement. |
9 |
Conditional expression |
A compact expression that chooses between two values. |
10 |
Iteration |
Repeating a block of code. |
11 |
Iterable |
An object that can provide its items one at a time. |
12 |
Iterator |
An object that tracks its position while producing items one at a time. |
13 |
|
A loop that repeats once for each item in an iterable. |
14 |
|
A built-in type commonly used to generate integer sequences for loops. |
15 |
|
A loop that repeats while a condition remains true. |
16 |
Sentinel |
A special value that signals when a data-controlled loop should stop. |
17 |
Infinite loop |
A loop whose condition never becomes false unless something interrupts it. |
18 |
|
A statement that exits the nearest enclosing loop immediately. |
19 |
|
A statement that skips the rest of the current loop iteration. |
20 |
Running total |
A variable updated inside a loop to accumulate a total such as total rainfall. |
21 |
Accumulator pattern |
A loop pattern that initializes a variable before the loop, updates it on each iteration, and uses the result after the loop. |
22 |
Trace table |
A table that records the value of each variable after every loop iteration, used to check how a loop behaves. |
23 |
Chained conditional |
A conditional statement with one or more |
24 |
|
A statement that does nothing, used as a placeholder where Python requires a block. |
25 |
Loop variable |
The variable in a |
26 |
Nested loop |
A loop inside the body of another loop; the inner loop finishes all its iterations for each iteration of the outer loop. |
27 |
Off-by-one error |
A bug where a loop or index runs one step too many or too few, often from using |
28 |
|
A built-in function that produces each item of an iterable together with its index. |
29 |
|
A built-in function that pairs up items from two or more iterables so you can loop over them in parallel. |