1.

Explain while loop structure with an example.

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(s);

}

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 executed.

Example:

#include<iostream>

int main ()

{\

int a = 10;

while(a<15)

{

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

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



Discussion

No Comment Found

Related InterviewSolutions