I will discuss few things here.
- Do while loop
- nested do while loop
- exercises
Do While:
Like While Loop which checks the condition at the top of the loop , in Do While loop condition is checked at the bottom of loop.
It works like While Loop but difference is, statements inside loop are executed for the first time without checking the condition.If the condition is wrong then no further executions.
do
{
statements;
}
while( condition );
Now you can see that from the syntax , conditional expression is at the end of the loop which clearly shows that statements are executed for the first time and then condition is checked , if condition is true then it jumps back to do and execute the statements till condition becomes false.
Example:
Let us take an example
i define one integer variable named x and assigned it a value 10.
In the do while loop , i define two statements , first one is display value of x and second one is increment the value of variable x by 2.
Then put the condition dislpay the values of x untill the condition which is display values of x less than 30.
My program code is this
Program Code:
Now you can see that from the syntax , conditional expression is at the end of the loop which clearly shows that statements are executed for the first time and then condition is checked , if condition is true then it jumps back to do and execute the statements till condition becomes false.
Example:
Let us take an example
i define one integer variable named x and assigned it a value 10.
In the do while loop , i define two statements , first one is display value of x and second one is increment the value of variable x by 2.
Then put the condition dislpay the values of x untill the condition which is display values of x less than 30.
My program code is this
Program Code:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + 2 ;
}
while( x < 30);
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + 2 ;
}
while( x < 30);
getch();
}
and the output of this program is this
Now see our condition is satisified here and its done. Keep practicing on it.
do
{
statements;
do
{
.
.
.
}while(condition);
}
while(condition);
Dots represent that you can use any number of do while loops depending upon you program.
Nested Do while loops
Syntax:do
{
statements;
do
{
.
.
.
}while(condition);
}
while(condition);
Dots represent that you can use any number of do while loops depending upon you program.
Hope that you will find this helpul.
Exercises:
1.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + 2 ;
}
while( x < 100);
getch();
}
2.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + 2 ;
}
while( x = 100);
getch();
}
3.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + 2 ;
}
while( x < 100);
getch();
}
4.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 1;
do
{
cout<< " value of x = " << x << endl ;
x = x + 3 ;
}
while( x <= 10);
getch();
}
5.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 1;
do
{
cout<< " value of x = " << x << endl ;
x = x + 3 ;
}
while( x > 10);
getch();
}
6.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + (2*2) ;
}
while( x < 30);
getch();
}
7.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + (10/2) ;
}
while( x < 30);
getch();
}
8.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 2;
do
{
cout<< " value of x = " << x << endl ;
x = x + (10/2) ;
}
while( x < 30);
getch();
}
9.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 2;
do
{
cout<< " value of x = " << x << endl ;
x = x + (10/2 + 2) ;
}
while( x < 30);
getch();
}
10.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 2;
do
{
cout<< " value of x = " << x << endl ;
x = x + (10/2 + x) ;
}
while( x < 30);
getch();
}
Exercises:
1.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + 2 ;
}
while( x < 100);
getch();
}
2.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + 2 ;
}
while( x = 100);
getch();
}
3.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + 2 ;
}
while( x < 100);
getch();
}
4.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 1;
do
{
cout<< " value of x = " << x << endl ;
x = x + 3 ;
}
while( x <= 10);
getch();
}
5.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 1;
do
{
cout<< " value of x = " << x << endl ;
x = x + 3 ;
}
while( x > 10);
getch();
}
6.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + (2*2) ;
}
while( x < 30);
getch();
}
7.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 10;
do
{
cout<< " value of x = " << x << endl ;
x = x + (10/2) ;
}
while( x < 30);
getch();
}
8.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 2;
do
{
cout<< " value of x = " << x << endl ;
x = x + (10/2) ;
}
while( x < 30);
getch();
}
9.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 2;
do
{
cout<< " value of x = " << x << endl ;
x = x + (10/2 + 2) ;
}
while( x < 30);
getch();
}
10.What is output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x;
x = 2;
do
{
cout<< " value of x = " << x << endl ;
x = x + (10/2 + x) ;
}
while( x < 30);
getch();
}
This comment has been removed by the author.
ReplyDeleteBro Assalam O Aliakum. Can we use like this???
ReplyDeleteint n;
while(n=1;n<=5;n++)
Or
while(int n;n=1;n<=5;n++)