+1 vote
in Class 12 by kratos

Explain while loop structure with an example.

1 Answer

+5 votes
by kratos
 
Best answer

A while loop statement repeatedly executes a statement or sequence of statements written within 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(*);

}

Here the condition may be any expression, and true for 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 ****.

Example:

include

int main ()

{\

int a = 10;

while(a<15)

{

cout<<"value of a:"<<a<<endl;

a++;

}

return 0;

}

when the above code is ****,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

...