1. Concept library
  2. Python Foundations
  3. Module 2: Control flow

Concept

while loops

A while loop repeats a block as long as its condition holds. Use it when you do not know in advance how many times to go round, such as reading input until the user quits.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain when a while loop fits better than a for loop
  2. Write a while loop that reads input until the user quits
  3. Spot the missing update that makes a loop never end

In practice

A few lines that show while loops doing its job.

Python
balance = 100
years = 0

while balance < 200:
    balance *= 1.07
    years += 1

print(years)   # 11

The common mistake

Watch for

Forgetting to change anything the condition depends on inside the loop, so it never ends. Every while loop needs a line that moves it toward stopping.

What to know first

Your roadmap puts these before while loops, unless you already know them.

How it stays learned

Once a session shows you understand while loops, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Conditionals
Next in the modulefor loops and range →