C++ while Loop Structure (with example programs)

- November 21, 2017
The "while" is a conditional loop structure. It is used to execute a statement or set of statements as long as the given condition remains true. This loop structure is used when the programmer doesn't know in advance the number of repetitions of loop. The loop is controlled by the user's given value.
The general syntax of "while" loop structure is given below:-
while (condition)
statement;
next_statement;
In the above syntax:
condition It represents the test condition. It may be a relational expression or logical expression. If it is true, the statement or set of statements given under the "while" loop is executed.
Statement It represents the body of the loop. If the body of loop contains more than one statement, then they are written within curly brackets "{   }".
The syntax of "while" structure for a group of statements is as follows:
while (condition)
{
group of statements;
}
next_statement;

Flowchart of 'while' Loop

Flowchart of the while loop is as follows:
C++ while loop flow chart

Working of while loop

When a 'while' loop statement is executed, the condition is evaluated first. If it is true, the body of loop is executed. After executing the body of loop, the execution control goes back to the "while" statement and evaluate the condition again. If the given condition is still true, the body of loop is executed again. This process is repeated again and again until the given condition remains true. When the given condition becomes false at any stage during execution, the loop is terminated (ended). After terminating the loop, the control shifts to the statement that comes immediately after the body of loop.
The body of the loop must contain a statement or o control the repetition of loop. Usually, a statement is given In the body of loop to change the value of control variable so that the condition becomes false during the execution of loop. If the condition of loop never becomes false, the loop will never terminate. In this case it will become an infinite loop.
To clear this concept look at the given below source code:
(Program in C++ that displays counting from 1 to 12 using 'while' loop.)
#include <iostream>
using namespace std;
main( )
{
int num;
n= 1;
while (n <= 12 )
{
cout<<"n<<endl;
num++;
}
cout<<"OK";
return 0;
}

Output of the program:

1
2
3
4
5
6
7
8
9
10
11
12
OK
In the above program:
The "n<=12" is a test condition. The value 1 is assigned to the variable "n" before the loop statement to make the condition true. This variable is used as control variable. When the condition is tested for the first time, it will return the true value, so the body of the loop will be executed. In the body of the loop, the "n++;" statement changes the value of the variable "n" during the program execution. After executing the body of the loop 12 times, the value of "n" becomes 13. By the condition "13<=12" the loop will terminate and control shifts to the statement "cout<<"OK";" that comes immediately after the "while" loop structure.

Examples Programs of C++ 'while' loop.

Example 1. Write a program that generates following series using the while loop
1,5,25,125,625
#include <iostream>
using namespace std;
main( )
{
long n;
n= 1;
while (n<=625)
{
cout<<n<<"\t";
n*=5;
}
return 0;

}
Output of the program:
1      5      25      125      625

Example 2. Write the program that displays five numbers and their sum using the 'while' loop.
#include <iostream>
using namespace std;
main( )
{
int c, s;
c = 1;
s = 0;
while (c<=5)
{
cout<<c<<endl;
s = s + c;
c++;
}
cout<<"Sum of five numbers = "<<s<<endl;
return 0;
}
Output of the program:
1
2
3
4
5
Sum of the five numbers = 15

Example 3: Write a program that displays the numbers which are divisible by 3 of first 15 natural numbers using 'while' loop statement.
#include <iostream>
using namespace std;
main( )
{
int num;
num=1;
while (num<=15)
{
if (num%3==0)
cout<<num<<endl;
num=num+1;
}
return 0;
}
Output of the program:
3
6
9
12
15

Example 4: Write a program that displays the squares and cube of numbers from 2 to 7 using the 'while' loop structure.
#include <iostream>
using namespace std;
main( )
{
int square, cube, n;
n=2;
cout<<"No.\t Square \t Cube \n";
while (n<=7)
{
cout<<n<<'\t'<<n*n<<'\t'\<<n*n*n<<'\t'<<endl;
n++;
}
return 0;
}
Output of the program:
No.      Square      Cube
2      4      8
3      9      27
4      16      64
5      25      125
6      36      216
7      49      343

Example 5: Write a program to input a number from user and check whether it is Prime number or not.
#include <iostream>
using namespace std;
main( )
{
int n, d, p = 1;
cout<<"Enter a Number:";
cin>>n;
d= 2;
while ( d< n)
{
if (n%d==0)
  {
    p = 0;
    break;
   }
d++
}
if (p==1)
  cout<<n<<" is a prime number";
else 
  cout<<n<<" is a composite number";
return 0;
}
Output of the program:
Enter a number: 13
13 is a prime number

Example 6: Write a program that inputs a number and checks whether the number is Fibonacci number or not.
#include <iostream>
using namespace std;
main( )
{
int a, b, next, num;
coout<<"Enter a Number:";
cin>>num;
if ( (num==0) || (num==1) )
cout<<num<<" is a Fibonacci number";
else
{
a= 0;
b= 1;
next= a+b;
while (next<num)
{
   a=b;
   b= next;
   next= a+b;
}
if (next==num)
  cout<<num<<" is a Fibonacci number";
else
  cout<<num<<" is not a Fibonacci number";
}
return 0;
}
Output of the program:
Enter a number: 17
17 is not a Fibonacci number

Example 7: Write a program that inputs 4 digit numbers and displays its digits on separate four lines.
For example, if the user enters 12345, the output should be displayed as follows:
1
2
3
4
5
#include <iostream>
using namespace std;
main( )
{
long r, n, d= 1000;
cout<<"Enter 4 digit number:";
cin>>n;
while (n!=0)
{
r=n/d;
n=n%d;
d=d/10;
cout<<r<<endl;
}
return 0;
}
Output of the program:
Enter 4 digit number: 2541
2
5
4
1

Example 8: Wrie a program that inputs different integer value in a single variable and find the largest value. The loop terminate when the user enters the value -1.
#include <iostream>
using namespace std;
main( )
{
long max, n=0;
max=n;
while(n!=-1)
{
cout<<"Enter any value:";
cin>>n;
if (max<n) max = n;
}
cout<<"The largest entered value is: "<<max;
return 0;
}
Output of the Program:
Enter any value:12
Enter any value:54
Enter any value:75
Enter any value:64
Enter any value:95
Enter any value:79
Enter any value:32
Enter any value:85
Enter any value:-1
The largest entered value is: 95

Example 9: Write a program that inputs a number and displays the factorial of that number using the 'while' loop.
#include <iostream>
using namespace std;
main( )
{
long fact, num, c;
cout<<"Enter any integer value:";
cin>>num;
fact= 1;
c= 1;
while (c<num)
{
  fact = fact*c;
  c = c+1;
}
cout<<"Factorial of "<<num<< "=" <<fact;
return 0;
}
Output of the Program:
Enter any integer value: 6
Factorial of 6 = 720

Example 10: Write a program that inputs an integer and displays the sum of its digits using the while loop. Such as, the program should display 26 if the user enters 26378.
#include <iostream>
using namespace std;
main( )
{
long r, n, m, sum= 0;
cout<<"Enter any integer value:";
cin>>n;
m= n; 
while ( n!= 0)
{ 
  r = n % 10;
  if (r==0)
  sum= sum + n;
else
  sum = sum + r;
  n= n\10;
}
cout<<"Sum of digits of "<<m<<" = " <<sum<<endl;
return 0;
} 
Output of the Program:
Enter any integer value: 2781
Sum of digits of 2781= 18

Example 11. Write a program that computes the sum of the following series using the 'while' loop.
1+1/2+1/4+1/6+1/8+................+1/100
#include <iostream>
using namespace std;
main( )
{
  float n, s;
  n= 2.0;
  s= 1.0;
  while (n<=100)
{
  s= s+ 1.0/n;
  n= n+2;
}
cout<<"Sum of series = "<<s;
return 0;
}
Output of the Program:
Sum of the series= 3.249602

Example 12. Write a program that inputs a number and displays its table using 'while' loop. Suppose if the user inputs number 5, the output should be displayed as follows:
    1 x 5 = 5
    2 x 5 = 10
    3 x 5 =  15
     ..............
    10 x 5 = 50
#include <iostream>
using namespace std;
main( )
{
int tab, n;
  cout<<"Enter a number:";
  cin>>tab;
  n = 1;
while (n<= 10)
{
  cout<<n<<'x'<<tab<<'='<<tab*n<<endl;
n = n+ 1;
}
return 0;
}