capypad
0 day streak
cpp / beginner
Snippet

Iterating with While Loops

A while loop continues to execute as long as its condition is true. It is useful when the number of repetitions depends on a dynamic state.

snippet.cpp
cpp
1
2
3
4
int energy = 3;
while (energy > 0) {
energy--;
}
Breakdown
1
while (energy > 0) {
Evaluates the condition; if energy is greater than 0, the code block inside is executed.
2
energy--;
Decrements the energy variable by 1 in each iteration to eventually stop the loop.