Beginner·5 min·basics · loops
While Loops & break/continue
While Loops
A while loop runs as long as a condition is true. Use it when you don't know up-front how many times you'll loop.
Two escape hatches
break— exit the loop immediatelycontinue— skip to the next iteration
Pitfall
Forgetting to update the condition variable → infinite loop. Always ask "what makes this stop?"
Try it
- Read user input in a loop until they type "quit". (Use a hardcoded list for now.)
- Skip negative numbers in a list with
continue.