While Loops
A while loop keeps going while a check is True.
Like waiting at a traffic light that stays red until it turns green.
You must change something inside the loop.
If the check never becomes False, the loop never stops.
Basic while
Write while, then a True/False check, then a colon.
Indent the body.
Update the value that the check uses.
Why the update matters
If you forget to change n, the check stays True forever.
That is an infinite loop.
Always ask, what will make this check become False?
while with a flag
A flag is a boolean that you flip when it is time to stop.
Like a Done sticker.
break and continue
break exits the while loop right away.
continue jumps back to the while check.
else on a loop
A for or while can have an else.
The else runs if the loop finished without break.
Think of it as, we searched the whole box and never found a stop.
for vs while again
for: known list or known count
while: keep going until a rule changes
while needs a careful update
both can use break and continue
Tip: Before you run a while loop, name the thing that will eventually stop it.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.