while
Loops as long as the condition is true.
Syntax
while (condition) { ... }Description
Used when the number of repetitions is unknown. Don't forget to break the condition at some point, or you'll get an infinite loop.
Example
Loading editor…
Expected output
0 1 2
When to use?
Indefinite repetition, waiting 'until correct input is entered'.
Common mistakes
- ⚠️ If you forget to increment like s++, it results in an infinite loop.