Do while and while loop

Do while and While loop in C++


In this post, we will learn the concept of while and do while loop in the c++ language.


In the programming language, loops are used to execute the block of code again.


There are three types of loops in the c++ language.

  1. For loop

  2. While loop

  3. Do while loop

In the for loop, we were discussing in the previous tutorials about the concept of for loop.


While loop

In the while loop, initialize conditions come before the while

 loop starts, while the statement has the condition of the terminated condition of the loop.

What is the syntax of the while loop?


Syntax of while loop:


while(condition)

{

//code;

}

  • A while statement condition evaluates.

If the condition true, then the body of the loop evaluates.
  • The condition again executes.

  • This process continues untill , condition false.

  • When the condition of while is false then the loop terminated.



Example:
//find the factorial of the number which user factorial of the number user wants
Find the factorial.

#include<iostream>
using namespace std;
int main()
{
int fact,factorial=1;
cout<<" Enter the number of which factorial find";
cin>>fact;
int i=2;
while(i<=fact){
factorial*= i;
i++;
}
cout<<" factorial of "<<fact<<" is "<<factorial<<endl;
return 0;
}

OUTPUT:
Enter number is 5
120

  • In this code, take a number from the user.
  • In the first alteration value i is 2,it multiple by the factorial value's 1,result is factorial=2;
  • In the 2nd alteration, i value is 3,it multiple by factorial, result factorial=6.
  • In the 3rd alteration, i value is 4,it multiple by factorial,result factorial=24.
  • In the 4th alteration, i value is 5,it multiple by factorial,result factorial=120.
  • The value of 6 which false the condition.
Example2

#include<iostream>
using namespace std;
int main()
{
int number;
int sum=0;
//take input from the user
cout<<" enter the number ";
cin>>number;
while(number>0)
{
//summation the number in every alteration
sum+=number;
//decrement in the number
number--;
}
cout<<" sum of number"<<sum<<endl;

return 0;

}

Output:

enter the number 5
sum of number 15


In the above code,while loop is use for the summation of the number .This is done by the decrement in the number and add in the sum variable in every iteration. The process continues until the number is equal to zero.

Do while loop

In the do-while loop, things are little changes from the while loop. First, do statement executes then while the condition is checked.






Syntax:while loop



Do{


//code


}while(condition);


  • The body of the loop executes first then condition of the loop check.

  • If the condition of while true then body of loop again executes.

  • This process continues untill condition false, then the loop stop.


Example :
 Same example done by do-while loop.
//find the factorial of the number which user factorial of the number user wants

#include<iostream>
using namespace std;
int main()
{
int fact,factorial=1;
cout<<" Enter the number of which factorial find";
cin>>fact;
int i=2;
//use the do while loop


do{
factorial*= i;                //counter variable multiple with factorial in every iteration
i++;
}while(i<=fact);
cout<<" factorial of "<<fact<<" is "<<factorial<<endl;                //display the factorial variables

}

OUTPUT:
Enter number is 5
120

  • In this code, take a number from the user.
  • In the zero alteration value i is 2,it multiple by the factorial value's 1,result is factorial=2;
  • In the 1st alteration, i value is 3,it multiple by factorial, result factorial=6.
  • In the 2nd alteration, i value is 4,it multiple by factorial,result factorial=24.
  • In the 3rd alteration, i value is 5,it multiple by factorial,result factorial=120.
  • The value of 6 which false the condition.
Example2

#include<iostream>
using namespace std;
int main()
{
int number;
int sum=0;
//take input from the user
cout<<" enter the number ";
cin>>number;
do
{
//summation the number in every alteration 

sum+=number;
//decrement in the number

number--;
}while(number>0);
cout<<" sum of number"<<sum<<endl;                //display the sum variable

return 0;

}

Output:

enter the number 5
sum of number 15

In the do-while loop, the first time must execute then reach the condition. Add the number in the sum and decrement in the number. This process continues until the number becomes 0.

Do-while loop is very helpful in menu-driven programming. The property of do-while first always execute makes a helpful do-while loop in the menu-driven programming.Take one example for menu which  is elaborate the use of do-while loop

How to use the Do-while loop for the menu-driven program?

For Example:

#include<iostream>
using namespace std;
int main()
{
int number;


//use the do-while for the menu-driven program and write the menu in the body of the loop
do
{
cout<<" select the option"<<endl;
cout<<" enter 1 for addition"<<endl;
cout<<" enter 2 for subtraction"<<endl;
cout<<" enter 3 for multiplication"<<endl;
cout<<" enter 4 for division"<<endl;
//take input from the user
cin>>number;

}while(number!=1 && number!=2 && number!=3 && number!=4);            //condition for do-while loop
cout<<" your option "<<number<<endl;                    //display your choice

return 0;

}

Output:
select the option
enter 1 for addition
enter 2 for subtraction
enter 3 for multiplication
enter 4 for the division

your option 4



In the above example, the program takes input from the user until the user enters the correct number.
First-time menu display in front of the user, if the user enters wrong selection then loop start and display the message for selecting the option which is written in the menu. In this way, the do-while loop is very handful for writing the program in the menu-driven programming.

Please write the comment,if you find anything incorrect,any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout