The while-statement executes code until the condition becomes false.
The syntax for the while-loop is:
while (condition) { // execute some code }
As long as the condition is true, the while-loop will continue executing the code.
When the condition becomes false, the while loop terminates.
Example:
#include <iostream> int main() /*from w ww.ja va 2 s .co m*/ { int x = 0; while (x < 10) { std::cout << "The value of x is: " << x << '\n'; x++; } }
The code in this example executes ten times.
After each iteration, the condition x < 10 is evaluated, and as long as it is equal to true, the code in the code block will keep executing.
Once the condition becomes false, the while loop terminates.
In this example, we increment the value of x in each iteration.
And once it becomes 10, the loop terminates.