Some problems require that a set of statements be executed a number of times  each time changing the values of one or more variables  so that every new execution in is different from the previous one this kind of repetitive execution of a set of the statement in a program is known as loop 



we can categorize loop structure into two categories

  •  non-deterministic loops
  •  deterministic loop

 when the number of times the loaf is to be executed is not known then the loop is called a non-deterministic loop otherwise it is called a deterministic loop

 C++ force while do-while and for loop constructs to help operative executions of a compound statement in a program the while and do-while loops are not deterministic groups and the following is a deterministic loop

The while loop

it is the fundamental conditional repetitive control structure in C and C++.The general fform of this construct is given below:

                                                        while<cond> statement;

Where while is a reserved word of c++ ; <cond>is a boolean expression ; statement can be a simple or compount statment;

The sequence  of operation in a while loop is as follows:

  • Test the Statement.
  • If the condition is true than execute  the statement and repeat step 1
  • if the condition is false leave the loop and go on with the rest of the program
  • it performs a pre test before the body of the loop is allowed to execute


Example 

Write a program that computes the factorial of a number N

solution

#include<iostream>
using namespace std;
int main()
{
int N;
int fact;
cout<<"Enter the number";
cin>>N;
fact = N;
while(N>1)
{
N=N-1;
fact = fact *1;
}
cout<<"The factorial of"<<N<<"is=<<fact;
}

The do-while loop



It is another conditional repetitive control structure provided by C and C++ .The syntax of this construct is given below:

do
{
 

 statement;

}while<cond>;

where do is a reserved word ; statement can be simple or compount statement.while is a reserved word and condition is a boolean epression.

Example:

write a program that display the following menu to the user and ask for his and her choice 
Menu
Travel by air          1
Travel by train      2
Travel by bus        3
Travel by taxi       4
Quit                       5
Enter your choice:

Solution:
#include<iostream>
using namespace std;
int main()
{
int choice;
do
{
cout<<"Menu"<<endl;
cout<<"\tTravel by air 1"<<endl;
    cout<<"\tTravel by Train 2"<<endl;
    cout<<"\tTravel by Bus 3"<<endl;
    cout<<"\tTravel by Taxi 4"<<endl;
    cout<<"\t\t\tQuit 5"<<endl;
    cout<<"Enter your choice"<<endl;
    cin>>choice;
    switch(choice)
    {
    case 1:
    cout<<"Fast and Risky"<<endl;
    break;
    case 2:
         cout<<"Comfortable and safe"<<endl;
         break;
case 3:
     cout<<"Slow and jerky"<<endl;
     break;
case 4:
     cout<<"Quick and costly"<<endl;
             break;
}
}while(choice!=5);
}