Skip to content

Control Flow

What you'll learn

  • Branching with if/elif/else
  • Iteration with for and while
  • Loop control keywords

Explanation

Control flow decides which code runs and how often. Python emphasizes readability with explicit blocks and consistent indentation.

Example

for n in range(1, 6):
    if n % 2 == 0:
        print(n, "even")
    else:
        print(n, "odd")

Common mistakes

  • Infinite while loops due to missing state updates
  • Off-by-one errors with range

Exercises

See Foundations Exercises.

Further reading