You are on page 1of 4

While loop:

While loop is the simplest loop of c++


language. This loop execute one or more statement while
the given condition remains true. It is useful when the
number of iterations is not known is advance.

Syntax
The syntax of while loop is as follows:

While(condition)
{
statement 1;
statement 2;
:
:
Statement N;
}
Flowchart
The flowchart of while loop is as follows:

Condition Loop body

The example of While Loop

Write a program that displays counting from 1 to 10


using while loop.
The Program is:

#include<iostream>
using namespace std;
int main()
{
int n;
n=1;
while(n<=10)
{
cout<<n<<endl;
n++;

}
getch();
}
How above Program Works?

First of all, the condition is evaluated. If it


is True, the control enters the body of the loop and
executes all statements in the body. After executing the
statements, it again move to the start of the loop and
evaluated the condition again. This process continues as
long as the condition remains True. When the condition
becomes false, the loop is terminated. While loop
terminated only when the condition becomes false. A
Loop that has no end point is known as an INFINITE Loop.

You might also like