Junior Programmer study materials
Junior Programmer study resources

Junior Programmer: Control Flow: Conditionals, Loops, and Logical Decision Making

Junior Programmer · Work through your exam, one topic at a time.

All guide sections

Control Flow: Conditionals, Loops, and Logical Decision Making

Control flow is about directing execution: choosing between paths with conditionals and repeating work with loops. The exam tests whether you can trace a loop by hand, predict which branch runs, and recognise a condition that never terminates or a boundary that is off by one.

Conditionals

if/elif/else (or if/else if/else) evaluate top to bottom and stop at the first true condition. Order therefore encodes logic: checking score >= 80 before score >= 90 makes a 95 fall into the B bucket. Conditions use comparison and logical operators; in Python, empty containers, 0, None, and '' are falsy, while in Java only a boolean expression is allowed in a condition.

Loops

for loops iterate over a known sequence (range, list, string); while loops repeat while a condition holds and require something inside the body to change that condition. break exits the loop; continue skips to the next iteration. Ranges are usually half-open: range(1, 6) yields 1,2,3,4,5 and range(5) yields 0 through 4. Nested loops multiply their iteration counts, which matters both for output and for performance.

Worked example

for i in range(1, 6):
    if i % 2 == 0:
        continue
    print(i)

range(1, 6) produces 1 through 5. For i=1, 1 % 2 == 0 is false, so 1 prints. For i=2 the condition is true, continue jumps to the next iteration without printing. The output is 1, 3, 5.

Traps

  • Off-by-one: using <= with a zero-based index, or assuming range includes its endpoint.
  • Forgetting to update the loop variable in a while loop, producing an infinite loop.
  • Modifying a list while iterating over it, which skips elements.
  • A dangling else bound to the wrong if, or elif written as a separate if so two branches both run.
  • Confusing break (exit the loop) with return (exit the function).