while
Repeats as long as the condition is true.
Syntax
while (condition) { ... }Description
while is used when the number of repetitions is not known beforehand. Remember to break the condition at some point, otherwise, it becomes an infinite loop.
Example
Loading editor…
Expected output
0 1 2
When to use?
Conditional repetition.
Common mistakes
- ⚠️ Forgetting to increment (n++) creates an infinite loop.