while and do-while loop
2. While Loop​
- entry controlled loop
- pre-tested
- executes atleast zero times when condition is false .
syntax:
2. while(condition)
{// loop body start
} // loop body ends
Example 1 :
#include <iostream>
using namespace std;
int main() {
int i=1;
while(i<=5)
{
cout<<i<<"\n";
i++;
}
return 0;
}
Output :
1
2
3
4
5
Explanation :
In a Line no 6
we declare one variable which name has i
and store value 1
, datatype is integer
.
In a Line no 7
while(i<=5)
while
is keyword, check condition i<=5
.
In a Line no 8
Start body of while loop .
In a Line no 9
print i
variable using cout
\n
is used to new line.
In a Line no 10
increase value of i
by one.
In a Line no 11
ends body of while loop .
Example 2 :
#include <iostream>
using namespace std;
int main() {
int i=0;
int n;
cout<<"Enter n :"<<"\n";
cin>>n;
while(i<n)
{
cout<<i<<"\n";
i++;
}
return 0;
}
Output :
0
2
3
.
.
.
9
Explanation :
In a Line no 6
we declare one variable which name has i
and store value 1
, datatype is integer
.
In a Line no 7
we declare another variable which name has n
.
In a Line no 9
Enter n :
from user.
In a Line no 10
store value of n
variable using cin
object.
In a Line no 11
while(i<=5)
while
is keyword, check condition i<=5
.
In a Line no 12
Start body of while loop .
In a Line no 13
print i
variable using cout
\n
is used to new line.
In a Line no 14
increase value of i
by one.
In a Line no 15
ends body of while loop .
Example 3 :
#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter value of n: ";
cin>>n;
int i=1;
while(i<=n)
{
cout<<i<<endl;
i++;
}
return 0;
}
Output :
Enter value of n: 25
1
2
3
.
.
.
25
Explanation :
In a Line no 6
we declare one variable which name has i
and store value 1
, datatype is integer
.
In a Line no 7
we declare another variable which name has n
.
In a Line no 9
Enter n :
from user.
In a Line no 10
store value of n
variable using cin
object.
In a Line no 11
while(i<=5)
while
is keyword, check condition i<=5
.
In a Line no 12
Start body of while loop .
In a Line no 13
print i
variable using cout
\n
is used to new line.
In a Line no 14
increase value of i
by one.
In a Line no 15
ends body of while loop .
3. do-while loop :​
- exit controlled loop
- post tested
- it executes atleast once even if condition is false .
syntax:
3. do
{// start
[example]
} // ends
while(condition);
Example 4 :
#include <iostream>
using namespace std;
int main() {
int i=10;
do
{
cout<<i<<"\n";
i++;
}
while(i<=5);
return 0;
}
Output :
10
Explanation :
In a Line no 6
we declare one variable which name has i
and store value 10
, datatype is integer
.
In a Line no 7
do
is a keyword.
In a Line no 8
Start body of do-while loop .
In a Line no 9
print i
variable using cout
\n
is used to new line.
In a Line no 10 increase value of
iby one.<br/>
In a
Line no 11ends body of while loop .<br/>
In a
Line no 12
while(i<=5)
whileis keyword, check condition
i<=5`.
Example 5 :
#include <iostream>
using namespace std;
int main() {
int i=10;
do
{
cout<<i<<"\n";
i++;
}
while(i<=10);
return 0;
}
Output :
1
2
3
4
.
.
10
Explanation :
In a Line no 6
we declare one variable which name has i
and store value 10
, datatype is integer
.
In a Line no 7
do
is a keyword.
In a Line no 8
Start body of do-while loop .
In a Line no 9
print i
variable using cout
\n
is used to new line.
In a Line no 10 increase value of
iby one.<br/>
In a
Line no 11ends body of while loop .<br/>
In a
Line no 12
while(i<=5)
whileis keyword, check condition
i<=5`.
Example 6 : WAP to simulate video game menu
1---> New Game
2---> Resume
3---> Settings
4---> Exit
#include <iostream>
using namespace std;
int main() {
int choice = -1;
char ch= 'y';
do
{
cout<<"####### Video Game #######"<< endl;
cout<<"1 => New Game : "<<endl;
cout<<"2 => Resume : "<<endl;
cout<<"3 => Settings : "<<endl;
cin>>choice;
if(choice == 1)
{
cout<<"New game is starting...";
}
else if(choice == 2)
{
cout<<"Continue from where you left...";
}
else if(choice == 3)
{
cout<<"Select Settings...";
}
cout<<"\n\n Do you want to continue ? (y/n)";
cin>>ch;
}
while(ch == 'y');
return 0;
}
Output :
####### Video Game #######
1 => New Game
2 => Resume
3 => Settings
1
New game is starting...
Do you want to continue ? (y/n)y
####### Video Game #######
1 => New Game
2 => Resume
3 => Settings
2
Continue from where you left..."
Do you want to continue ? (y/n)y
####### Video Game #######
1 => New Game
2 => Resume
3 => Settings
3
Select Settings...
Do you want to continue ? (y/n)n
Difference between for, while, do-while loop :
for | while | do-while |
---|---|---|
syntax: for(initialization;condition;inc/dec) | syntax: while(condition){ //statements; } | syntax: do{ //statements; } while(condition); |
Entry controlled loop | Entry controlled loop | Exist controlled loop |
It executes atleast zero times, when condition is false | It executes atleast zero times, when condition is false | It executes atleast once times, when condition is false |
The for loop is used for definite loops when the number of iteration is known | The while loop is used when the number of iteration is not known |