A ‘while’ loop statement repeatedly executes a statement or sequence of statements written in the flower brackets as long as a given condition returns the value ‘true’.
Syntax: The syntax of a ‘while’ loop in C++ is: while(condition)
{
statement (s) ;
}
Here the condition may be any expression, and for true is any non zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop. During the first attempt, when the condition is tested and the result is false, the loop body will be skipped and the first statement after the ‘while’ loop will be executed.
Example: #include<iostream>
int main ()
{
int a = 10;
while (a<15)
{
cout <<"value of a:"<<a<<end1;
a++;
}
return 0;
}
When the above code is executed, it produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14