Back

Section Summary: Control Flow

divider

Activities 1.10 – 1.17. Use this to review, or to fill in a definition you missed.


1. Booleans and Conditions

A comparison produces a boolean, and an if statement uses that boolean to decide whether to run a block. In Python, indentation is what marks the block.

Boolean
A value that is either True or False.
Comparison Operator
An operator like == or >= that compares two values and produces a boolean.
if Statement
Runs a block of code only when its condition is True.
Condition
The boolean expression an if statement checks.
Code Block
A group of statements, marked in Python by indentation.
Logical Operator
and, or, and not — combine or invert booleans.

2. Branching

An if can be extended into a chain. Only the first branch whose condition is true will run, and this is also where you first imported the random module.

else
Runs when every condition before it was False.
elif
Chains another condition onto an if — only the first true branch runs.

3. Nesting

A conditional can live inside another conditional, which lets you ask a follow-up question only when the first answer was yes.

Nesting
Placing one code block (like an if) inside another.
pass
A placeholder statement that does nothing — used where Python requires a block but you have nothing to put there yet.

4. while Loops

A while loop repeats as long as its condition holds. Something inside the loop has to change that condition, or it never ends.

Iteration
Repeating a block of code multiple times.
while Loop
Repeats its block as long as its condition is True.
Infinite Loop
A loop whose condition never becomes False, so it never stops.
Sentinel Value
A specific value a loop watches for to know when to stop.
Built-in Function
A function Python already provides, like round() — avoid naming variables the same thing.

5. for Loops

When you know how many times to repeat, a for loop is the better fit. range() supplies the numbers to count through.

for Loop
Repeats its block once for each value in a sequence.
range()
Generates a sequence of numbers for a for loop to count through.

6. Nested Loops

A loop inside a loop runs the inner block once for every combination — the inner loop finishes completely on each pass of the outer one.

Nested Loop
A loop written inside the body of another loop.
Outer Loop
The loop that contains another loop inside it.
Inner Loop
The loop nested inside another — it completes fully for every pass of the outer loop.