How To Use While Loop Using C++ Language in DEV C++

I will discuss two things here
  • while loop
  • nested while loop
  • Exercises
while Loop

It simply checks the condition , if it is true it executes the statements inside it until the condition is true. When the condition becomes incorrect it comes out of the loop.
If the condition becomes incorrect at the start , it does not executes the statements inside it , it executes the statements after the loop.

Syntax:

While(  condition )
{
     statements;
}

Here condition may be any expression and true is for any non zero value.
Statements may be a single statement or block of statements.


Example:
Look at this example. i define one integer variable x and assigns some value let say 0. I put the condition for x<10  , display the numbers less than 10.
And when condition becomes false it comes out of the loop and executes the statements after the while loop.
so i write the statement " i am out of the loop "



x++ is a increment after displaying one number it simply incremented by one.
endl  is simply like a enter buttuon in your keyboard means go to next line.

So now go to Execute option, select compile and run , .exe file  appears and here it is your output



Now see our condition is satisified here and its done. Keep practicing on it.


Nested while loop

Nested while loop is easy to learn if you learnt the while loop.

Syntax

while(condition)
{
   while(condition
   {
   .
   .
   .
   }
statements;
}

Dots represent that you can use any number of while loops
Example:
Lets takes an example on it.
i am using break statement here , otherwise as long as condition is true program will execute infinity times. In order to avoid infinity behavior , i am using this statement to execute only one time.

Program code:



Output: After compiling and executing the output will be



Hope that you will find this helpul.
Exercises:

1.what is output of following program ?

#include<iostream>
using namespace std;

int main ()
{
     int x = 0;

 while ( x > 10 )
 {
    cout<<   x  <<" ";

  x++;
 }

cout<<endl<<endl;

    cout<< "       i am out of the loop   ";


}

2.what is output of following program ?

#include<iostream>
using namespace std;

int main ()
{
     int x = 0;

 while ( x = 10 )
 {
    cout<<   x  <<" ";

  x++;
 }

cout<<endl<<endl;

    cout<< "       i am out of the loop   ";


}

3.what is output of following program ?

#include<iostream>
using namespace std;

int main ()
{
     int x = 0;

 while ( x <= 10 )
 {
    cout<<   x  <<" ";

  x++;
 }

cout<<endl<<endl;

    cout<< "       i am out of the loop   ";


}

4.what is output of following program ?

#include<iostream>
using namespace std;

int main ()
{
     int x = 0;

 while ( x >= 10 )
 {
    cout<<   x  <<" ";

  x++;
 }

cout<<endl<<endl;

    cout<< "       i am out of the loop   ";


}

5.what is output of following program ?

#include<iostream>
using namespace std;

int main ()
{
     int x = 0;

 while ( x >= 10 )
 {
    cout<<   x  <<" ";

  x--;
 }

cout<<endl<<endl;

    cout<< "       i am out of the loop   ";


}

6.what is output of following program ?

#include<iostream>
using namespace std;

int main ()
{
     int x = 0;

 while ( x < 10 )
 {
    cout<<   x  <<" ";

  x--;

 }

cout<<endl<<endl;

    cout<< "       i am out of the loop   ";


}

7.what is output of following program ?

#include<iostream>
using namespace std;

int main ()
{
     int x = 0;

 while ( x < 10 )
 {
    cout<<   x  <<" ";

  x--;
  break ;
 }

cout<<endl<<endl;

    cout<< "       i am out of the loop   ";


}

8.what is output of following program ?

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int i = 10 ;
int j = 20 ;

while(i<=10)
{
while(j=20)
{
cout<< "value of i = 10"<<endl;
cout<< "value of j = 20"<<endl<<endl;
break;
  }
  break;
}

cout<< "value of i is "<< i <<endl;
cout<< "value of j is "<< j <<endl;
getch();

}

9.what is output of following program ?

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int i = 10 ;
int j = 20 ;

while(i=10)
{
while(j<=20)
{
cout<< "value of i = 10"<<endl;
cout<< "value of j = 20"<<endl<<endl;
break;
  }
  break;
}

cout<< "value of i is "<< i <<endl;
cout<< "value of j is "<< j <<endl;
getch();

}

10.what is output of following program ?

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int i = 10 ;
int j = 20 ;

while(i>10)
{
while(j=20)
{
cout<< "value of i = 10"<<endl;
cout<< "value of j = 20"<<endl<<endl;
break;
  }
  break;
}

cout<< "value of i is "<< i <<endl;
cout<< "value of j is "<< j <<endl;
getch();

}



keep practicing on it.

2 comments:

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.