The while loop loops through a block of code as long as a specified condition is true:
while (condition) {
// code block to be executed
}
Example:
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
In the example we print value of i and then increase it by 1 until it is less than 5. After i=4 we do i++ again, but this time the condition in while will be false, so the loop will not run again.
Comments
Leave a comment