A while
loop continues running while a condition is true, and stops when the condition becomes false.
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
A variation of the while
loop is the do-while
loop. The difference here is that the condition to check that the loop should continue is evaluated at the end of code block instead of at the beginning of the code block.
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);