Lesson 016

Control Flow: Loops

for · while · nested

1:00

How for and while loops automate repetition — including iteration counting, the half-open range, break/continue, nested loops, and the classic off-by-one and infinite-loop pitfalls.

By the end, you can

  • Explain the difference between definite and indefinite iteration.
  • Trace a `for` loop over `range(n)`, listing every value the loop variable takes and the exact iteration count.
  • Identify the off-by-one mistake in a `range` call and correct it.
  • Trace a `while` loop step by step, and explain why a missing increment causes an infinite loop.
  • Describe what `break` and `continue` each do, and distinguish their effects.
  • Calculate the total iteration count of a nested loop (outer × inner).
  • Choose between `for` and `while` given a problem description.
Up next in Programming Foundations
Questions or feedback?