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.
If the condition becomes incorrect at the start , it does not executes the statements inside it , it executes the statements after the loop.
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.
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
Nested while loop
Nested while loop is easy to learn if you learnt the while loop.
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 ";
}
keep practicing on it.
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 ";
}
#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 ";
}
#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 ";
}
#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 ";
}
#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 ";
}
#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();
}
#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();
}
#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();
}
#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.
lypfuPla_za Alexis Alexander DesignCAD 3D Max
ReplyDeleteCyberLink YouCam
Cinema 4D
burghandtesba
caltiWgia_be_1989 Thomas Newman There
ReplyDeleteClick here
maesuncopa