WHILE AND DO WHILE LOOP in C++

Page Content:

while loop in C++

While Loop in CPP
While Loop

A repetition structure(also called a looping structure or a loop) allows the programmer to specify that a program should repeat an action while some condition remains true. The pseudocode statement

While there are more items on my shopping list
Purchase next item and cross it off my list

describes the repetition that occurs during a shopping trip. The condition, “there are more items on my shopping list ” is either true or false. If it is true, then the action, “Purchase next item and cross it off my list” is performed. This action will be performed repeatedly while the condition remains true. The statement contained in the while repetition structure constitutes the body of the while, which can be a single statement or a block. Eventually, the condition will become false(when the last item on the shopping list has been purchased and crossed off the list). At this point, the repetition terminates, and the first pseudocode statement after the repetition structure executes

As an example of an actual while, consider a program segment designed to find the first power of 2 larger than 1000. Suppose the integer variable product has been initialized to 2.When the following while repetition structure finishes executing, the product will contain the desired answer:

int product=2;
while(product<=1000)
product =2*product;

When the while structure begins execution, the value of the product is 2. Each repetition of the while structure multiples product by 2, so product takes on the values 4,8,16,32,64,128,256,512 and 1024 successively. When a product becomes 1024, the while structure condition, product <=1000, becomes false. This terminates the repetition of the final value of the product is 1024. Program execution continues with the next statement after the while.

Note* Not providing in the body of the while structure, an action that eventually causes the condition in the while to become false normally results in a logic error called an infinite loop, in which the repetition never terminates

The syntax for while loop in C++ is given below:

while(condition)
 {
 statements;
 }

1.Print n natural number by using a while loop

#include<iostream> 
using namespace std;
  int main()
  {
  int i=1;
  while(i<=10) 
 { 
 cout<<i <<"\n"; 
 i++;
  }
  } 

OUTPUT:

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10 

2.Class average problems pseudocode algorithm that uses counter controlled repetition

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
//function main begins program execution
int main()
{
int total;
int gradeCounter;
int grade;
int average;
//initialization phase
total=0;
gradeCounter=1;
while(gradeCounter<=10)
{
cout<<"Enter grade:";
cin>> grade;
total=total+grade;
gradeCounter=gradeCounter+1;
}
average=total/10;
cout<<"Class average is "<< average<< endl;
return 0;
} //end function main

OUTPUT:

 Enter grade: 98
 Enter grade: 76
 Enter grade: 71
 Enter grade: 87
 Enter grade: 83
 Enter grade: 90
 Enter grade: 57
 Enter grade: 79
 Enter grade: 82
 Enter grade: 94
 Class average is 81

Nested while loop in C++

Nested while loop in C++ can be defined as while loop inside the while loop

The syntax for a nested while loop

while(condition)
 {    
while(condition) 
{       
statement(s);    
}    
statement(s); // you can put more statements. 
} 

1.C++ program to print the number pattern.

 1
 1 2
 1 2 3
 1 2 3 4
 1 2 3 4 5
#include<iostream>
#include<conio.h>
using namespace std;
 int main()
 {
     int i=1,j;
     while (i <= 5)
     {
         j=1;
         while (j <= i )
         {
             cout <<j;
             j++;
         }
         cout << endl;
         i++;
     }
     getch();
     return 0;
 }

OUTPUT:

 1
 1 2
 1 2 3
 1 2 3 4
 1 2 3 4 5 

2.Nested while loop example

 #include<iostream> 
 using namespace std;
  int main ()
  {
  int i=1; while(i<=3)
  {
  int j = 1;
  while (j <= 3)
  {
  cout<<i<<" "<<j<<"\n";
  j++;
  }
  i++;
  }
  } 

OUTPUT:

1 1
1 2
1 3
2 1
2 2 
2 3
3 1
3 2
3 3

Do-while loop in C++

Do-While in CPP
Do-While

The do-while repetition structure is similar to the while structure. In the while structure, the loop-continuation condition test occurs at the beginning of the loop before the body of the loop executes. The do-while structure tests the loop-continuation condition after the loop body executes; therefore, the loop body executes at least once. When a do-while terminates, execution continues with the statement after the while clause. Note that it is not necessary to use braces in the do-while structure if there is only one statement in the body; however, most programmers include the braces to avoid confusion between the while and do-while structure. For example,

 while(condition)

normally is regarded as the header to a while structure. A do-while with no braces around the single statement body appears as

do
statement
while(condition)

which can be confusing. The last line while(condition); might be misinterpreted by the reader as a while structure containing an empty statement. Thus, the do-while with one statement is often written as follows to avoid confusion:

do
{
statement
}while(condition);

1.Do-while loop simple program

#include<iostream>
using std::cout;
using std::endl;
int main()
{
int counter=1;
do{
cout<< counter << " ";

}while(++counter<=10);
cout<<endl;
return 0;
}

OUTPUT:

1 2 3 4 5 6 7 8 9 10

2. Display array elements using an array

#include <iostream> 
using namespace std; 
int main()
{    
int arr[]={21,99,15,109
};   
int i=0;    
do
{       
cout<<arr[i]<<endl;       
i++;    
}while(i<4);    
return 0; 
} 

OUTPUT:

21 
99 
15 
109 

Nested Do-while loop in C++

Nested while loop in C++ can be defined as a do-while loop inside the do-while loop

The syntax for nested do-while loop in C++

do
{ 
do
{

}while(condition1);

}while(condition2);  

1.Nested do-while loop in C++

 #include<iostream> 
 using namespace std; 
 int main() { 
 int i = 1; 
 do{ 
 int j = 1; 
 do{ 
 cout<<i<<"\n"; 
 j++; 
 } while (j <= 3) ; 
 i++; 
 } while (i <= 3) ; 
 } 

OUTPUT:

 1 1
 1 2
 1 3
 2 1
 2 2 
 2 3
 3 1
 3 2
 3 3 

Differences between while,do-while loop and for loop

while loopdo-while loop
Syntax of while loop: while(condition)
{


statements;



}
Syntax of do-while loop: do
{


statements;


}while(condition);
This is called as a entry controlled loop, as the entry inside the loop is possible only if the condition is true.This is called as exit controlled loop, as the entry inside this loop is sure i.e. no condition is checked to enter inside the loop.Nut the exit is possible only if the condition is false.
If the condition is not true for the first time, the control will never enter into the loop.Hence there is a possibility that the control never enters into the loop and the statements inside the loop are never executed.Even if the condition is not true for the first time the control will enter into the loop.Hence the statements inside the loop will be executed atleast once even if the condition is not true for the first time.
There is no semicolon (;) after the condition in the syntax of the while loop.There is a semicolon(;) after the condition in the syntax of the do-while loop.
The initializations and the updating is not a part of the syntax.The initializations and the updating is not a part of the syntax.

Note* Keep in mind the following guidelines when you design a loop:

  • Identify the condition that terminates the loop execution
  • Initialize that condition before the first test
  • Update the condition in each loop cycle before the condition is tested again

One nice thing about for loops is that their structure provides a place to implement these three guidelines, thus helping you to remember to do so. But these guidelines apply to a while loop, too

Translate »