While Loop
A while loop keeps going as long as a test is true.
You check first. Then you run. Then you check again.
Think of eating cookies while the jar still has cookies.
while
Updating the condition
do while
Avoid forever loops
Basic while
Write while (condition) { ... }.
Something must change
Inside the loop, update the value that the test uses.
If nothing changes, the loop may never stop.
Unknown counts
while is nice when you do not know the exact count at the start.
You just know the stop rule.
do while
do while runs the block at least once.
Then it checks the condition.
while vs for
for is great when start, stop, and step are clear.
while is great when the stop rule is a state that changes over time.
Tip: Before you run a while loop, point to the line that moves you closer to stopping.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.