4 Control Flow and Logic
A progressive guide to Python control flow, explaining sequence, conditionals, Boolean logic, loops, nesting, tracing, and reliable debugging practices.
The Foundations of Control Flow
Control flow is the order in which a program’s instructions execute. By default, statements run from top to bottom, but control-flow structures can select between alternatives, repeat instructions, or combine these behaviors. This allows an algorithm to respond to data rather than perform the same fixed every time.
A executes statements in order, once each. For example, a program can assign a price, assign a quantity, calculate a total, and then display the result. If the price is , the quantity is , and the calculation is , the displayed total is . If a variable is updated from to , a later statement uses the new value . The meaning of a program therefore depends on both the instructions it contains and their execution order.
The main control-flow patterns build on this default:
Selection chooses among alternatives.
repeats a process.
Nesting places one control structure inside another.
follows the actual execution path.
Takeaway: Execution order matters, and control-flow structures determine which instructions run, how often they run, and when they stop.
Selection with Conditionals
A chooses a branch based on a condition. Python tests if conditions from top to bottom. The first true branch runs, and the remaining branches are skipped. An elif branch supplies another condition, while an else branch handles the case in which no earlier condition is true.
Common comparisons include equality , inequality , less than , greater than , less than or equal to , and greater than or equal to . Assignment stores a value; it is different from the equality test . In Python code, these concepts appear through operators such as =, ==, and >=.
For example, a grading decision can test whether a score is at least , then at least , and then at least . A score of fails the first two tests, passes the third test, and receives the corresponding grade.
Python uses indentation to show which statements belong to each branch. Conditions should be ordered deliberately, especially when several ranges overlap. Test both the true and false outcomes, including boundary values such as the exact threshold.
Takeaway: A selects one execution path, so branch order, comparison choice, indentation, and boundary handling all matter.
Logic and Safe Conditions
A value is either True or False. expressions combine comparisons and control decisions.
andis true only when both expressions are true.oris true when at least one expression is true.notreverses a value.
Parentheses make compound conditions easier to read. For example, a safety decision can be written as , where the first alternative is true when the temperature is below zero and the second requires both snowfall and icy roads.
can prevent unnecessary or unsafe work. In a condition such as , the division is attempted only if the denominator is not zero. If the first part is false, the and expression is already known to be false. Similarly, an or expression may stop after a true first part.
Different languages use different notation, such as , , and , but the logical ideas are the same. Some languages, including Python and JavaScript, also allow certain non- values to behave like false in conditions; an empty collection and zero are common examples.
Takeaway: Build complex decisions from clear expressions, use parentheses when grouping may be unclear, and use short-circuit behavior to guard operations when appropriate.
with Loops
repeats a block of statements. Use a for loop when processing each item in a or a known range. In Python, range includes its starting value but excludes its ending value: range(1, 5) produces , , , and .
A loop can maintain an accumulator. Starting with and adding each value from through produces the final total . The accumulator is updated once per . This pattern is useful for sums, counts, products, and other running results.
Use a while loop when repetition should as long as a condition remains true. The condition is tested before each , so the loop needs an initial state, a condition, and an update that moves toward termination. If the update never makes the condition false, the result is an .
A statement ends the nearest loop immediately. A statement skips the rest of the current and starts the next one. For example, a loop can stop before printing a particular value with , or skip even values with . Use these statements carefully because excessive early exits can make a program harder to understand.
Takeaway: Choose for for - or count-oriented repetition and while for condition-oriented repetition; always make the stopping behavior explicit.
Nesting and Execution Cost
A contains one control structure inside another. A inside a loop is evaluated once per loop . In nested loops, the inner loop completes all of its iterations for each of the outer loop.
If two nested loops each run approximately times, the inner statement may execute approximately times. This makes nested loops useful for grids, tables, and comparing every item in one collection with every item in another, while also increasing the amount of work.
Nesting can sometimes be replaced with a compound condition. For example, checking that a username is not empty and a password is not empty can be written as one condition using and, rather than as two deeply nested conditionals. Clear indentation is essential in either form.
When nested code, follow the outer structure first. For each outer-loop , complete every inner-loop before returning to the outer loop.
Takeaway: Trace nested structures from the outside inward, keep indentation consistent, and simplify nesting when a direct expression communicates the same decision.
, Testing, and Debugging
follows execution one statement at a time. A reliable trace records the initial values, assignments, condition results, loop variables, and output in the order they occur.
Use this process:
Write down the initial value of every relevant variable.
Read one executable statement at a time.
Record each assignment immediately.
Evaluate every condition as
TrueorFalse.Follow only the selected branch.
For each loop , record the loop variable and the values changed by the body.
Record output when it occurs.
For a loop that adds only even numbers from through , the running total begins at , remains after the first odd value, becomes after adding , remains unchanged at after the next odd value, and ends at after adding .
When designing or debugging, check zero items, empty input, the first value, the last value, and values exactly on a boundary. Common errors include confusing assignment with comparison, forgetting a while update, using incorrect indentation, ordering conditions incorrectly, and misunderstanding the excluded endpoint of range.
A small hand trace can reveal incorrect conditions, updates, boundaries, and branch choices before program execution does. Test both sides of every branch and trace a small example before relying on a larger input.