MASIGNASUKAv102
6510051498749449419

C++ While loop - Learn With AVRK

Add Comments
Friday, August 27, 2021

While Loop in C++ Language 

In C++, while loop is used to repeat a block of the program several times. If the number of repetition is not fixed, It is suggested to use while loop rather than for loop.

Syntax -

while(condition)  
{
 /*if condition is true then code to be executed*/
}


Flowchart -

Loading...

C++ While Loop Example -

Let us see a example using while loop to print the table of 1.
#include <iostream>
using namespace std;
int main()  
{
int i=1;
    while(i<=10)
    {
    cout<<i <<"\n";
    i++;
    }
}


Output -

Loading...

C++ Nested While Loop Example -

In C++, when we use while loop inside another while loop it is called as nested while loop. The inner while loop is executed fully when outer loop is executed once.

Let us see a example of nested while loop in C++ programming.
#include<iostream>
using namespace std;
int main()  
{
int i=1;
   while(i<=3)
   {
    int j = 1;
    while(j<=4)
    {
    cout<<i<<" "<<j<<"\n";
    j++;
    }
    i++;
   }
}


Output -

Loading...

C++ Infinite While Loop Example -

We can also create infinite while loop by passing true value to the test condition.
#include<iostream>
using namespace std;
int main()  
{
int i=2;
   while(i>1)
   {
    cout<<"Infinite Loop"<<endl;
   }
}


Output -

Loading....
Also check out this : C++ For Loop - Learn With AVRK
Learn With AVRK

Learn With AVRK