🧩While Loop
Also known as while
A loop that keeps going while a test is true, and stops when it turns false.
Think of it like
Think of it like stirring soup while it is still lumpy. You stop the moment it is smooth.
Example
A rocket game counts down with a while loop. It prints 3, 2, 1, then the test n > 0 turns false and it prints Go!
Why it matters
Definition
Example code
python
n = 3
while n > 0:
print(n)
n = n - 1
print("Go!")Common mistake
Key takeaways
- 1Test before each pass. It checks, then runs, then checks again.
- 2Unknown count. Use it when you cannot count passes ahead.
- 3Change the test. Something inside must move it toward false.