Unit 4: Control Flow
How a program picks one path over another, and the boolean logic underneath every branch.
5 topics · 18 questions
Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.
if / else Branching
- How it worksHow does the computer test a condition once and commit to exactly one branch?
- Worked exampleTrace what prints for check_age(15) when the code prints minor if age < 18 else adult.
- Common mistakeWhy doesn't an else block ever run in the same pass as the if block right above it?
Your turn1 of 2
A program checks age < 24 to decide minor versus adult. Out of 133 people tested, 8 are under 24. How many are classified as adult?Nested and Chained Conditionals
- How it worksHow does an elif chain check conditions in order and stop at the first one that's true?
- Worked exampleTrace grade(72) through if score > 90: A, elif score > 80: B, elif score > 70: C, else: D.
- Worked exampleTrace grade(95) through the same elif chain and find which branch runs first.
- Common mistakeWhy doesn't reordering the branches of an elif chain leave the results exactly the same?
- Going furtherWhat happens when two elif conditions in the same chain could both be true?
Your turn1 of 2
An elif chain checks 8 conditions in order and stops at the first one that's true, which happens to be the 2-th condition checked. How many conditions get checked in total before that chain stops?Boolean Logic and Truth Tables
- How it worksHow does a truth table list every combination of true and false for a and not b?
- Worked exampleBuild the truth table for a and not b across all four combinations of a and b.
- Going furtherWhat happens to a truth table once a third variable joins a two-variable expression?
Your turn1 of 2
A truth table for 7 boolean variables lists every combination of true and false, giving 2 to the power of n rows. How many rows does a table for n variables have?Guard Clauses and Early Return
- How it worksHow does a guard clause exit a function immediately instead of wrapping the rest in an else block?
- Worked exampleTrace safe_divide(10, 0) written with a guard clause that returns None the moment the denominator is 0.
- Common mistakeWhy isn't an early return the same thing as a function silently failing?
- Going furtherWhat changes about a function's readability once three nested if statements become three guard clauses?
Your turn1 of 2
A function has 8 nested if statements rewritten as n guard clauses instead. How many total conditions does the function still check, regardless of which style is used?Multi-Way Branching with match / switch
- How it worksHow does a match statement compare one value against several patterns and jump to the one that fits?
- Worked exampleTrace what a match on day = 3 returns when case 1 is Monday, case 2 is Tuesday, case 3 is Wednesday.
- Going furtherWhat happens in a match statement when none of the listed cases fit the value?
Your turn1 of 2
A match statement compares one value against 8 listed case patterns, checking them in order. If the value matches the 2-th pattern checked, how many patterns are checked in total before the match statement finds it?