Control Flow

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, and else

  • for loops for iterating over data

  • while loops for condition-based repetition

  • Loop-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:

  1. Use conditional statements to choose between branches of code

  2. Write for and while loops for repeated work

  3. Use break, continue, and pass intentionally

  4. Trace 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 True or False.

4

Branch

One possible path through a conditional statement.

5

if statement

A conditional statement that runs a block only when its condition is true.

6

else clause

The branch that runs when the if condition is false.

7

elif clause

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

for loop

A loop that repeats once for each item in an iterable.

14

range

A built-in type commonly used to generate integer sequences for loops.

15

while loop

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

break statement

A statement that exits the nearest enclosing loop immediately.

19

continue statement

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 elif clauses that tests conditions in order and runs only the first true branch.

24

pass statement

A statement that does nothing, used as a placeholder where Python requires a block.

25

Loop variable

The variable in a for loop header that holds the current item on each iteration.

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 <= where < is needed.

28

enumerate

A built-in function that produces each item of an iterable together with its index.

29

zip

A built-in function that pairs up items from two or more iterables so you can loop over them in parallel.

Chapter Overview Slides