While Loop in C++


Prev Tutorial Next Tutorial

While Loop in C++

In while loop First check the condition if condition is true then control goes inside the loop body other wise goes outside the body. while loop will be repeats in clock wise direction.

Syntax

Assignment;
while(condition)
{
statements;
............
Increment or Decrements (++ or --);
}

Note: If while loop condition never false then loop become infinite loop.

When while loop is use ?

When we do not know about how many times loops are perform or iteration of loop is unknown.

Flow Diagram

While Loop

Example of while Loop in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int i;
clrscr();
i=1;
while(i<5)
{
cout<<endl<<i;
i++;
}
getch();
}

Output

Output:
1
2
3
4

Execution process of while loop is slower than for loop.


Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements