C++ Loop Types

Looping in C++

If we know the number of iterations to be performed, then we prefer to use for statement to perform the loop. The syntax of for statement is as below


If we need to execute a certain block of statements again and again for some number of times, then we need to perform a loop. C++ offers three kinds of statements that can be used to perform a loop.

• for
• while
• do-while

for statement

If we know the number of iterations to be performed, then we prefer to use for statement to perform the loop. The syntax of for statement is as below

for (expression1;expression2;expression3)
{
statements;
.
.
}

where
expression1 is initialization. In this part of for loop, we initialize the variable used to control the loop..
expression2 is a condition. The loop will continue as for as this condition is true.
expression3 is an increment or decrement statement. This expression shows that what will be the value of control variable for next iteration.

The body of for loop is written with the curly brackets if there are more than one statements to be repeated for some number of times. If there is only one statement in the body of the loop then the curly brackets are optional.

Example 1: Display all the values between 1 – 10.

#include “conio.h”
#include “iostream”
using namespace std;

void main()
{
for(int i=1; i<=10; i++)
{
cout<<i;
cout<<endl
}
getch();
}

In the above program, for loop is used to display all the integer values from 1 to 10. First, variable i is defined and initialized to 1. As it is less than 10, so the body of the loop will be executed to display the value of i and change the line. For the next iteration, i is incremented by 1. As the new value of i (i.e. 2) is still less than 10, so the body of the loop is executed again. This process continues until the condion becomes false. i.e. when the value of i becomes greater than 10.

The output of the above programs is as below.

1-10

Example 1: Display all the values between 1 – 10.

#include “stdafx.h”
#include “conio.h”
#include “iostream”
using namespace std;

void main()
{
int n;
cout<<“Enter an integer value… “;
cin>>n;
int fact=1;
int i;
for(i=n; i>=1; i–)
fact*=i;
cout<<n<<“!= “<<fact<<endl;
getch();
}

The output of the above program is as below.

120

Kindly note that there are no curly brackets after the for statement as there is only one statement in the body of the loop. Secondly, note the decrement operator in for statement to decrement the value of i before the next iteration. You can also see that variable i is declared before the for statement. If this case it is not required to be declared again within the for statement.

while statement

This is another loop statement available in C++. Although it can be used as a replacement of for statement but is preferred to use in situations when the number of iterations is not known.

The syntax of while statement is as below.

while (condition)
{
statements;
.
.
}

The statement within the body of while loop are executed as long as the condition is true. The while loop terminates as soon as the condition becomes false.

Example 1: Display all the values between 1 – 10.

#include “conio.h”
#include “iostream”
using namespace std;

void main()
{
int i=1;
while (i<=10)
{
cout<<i<<endl;
i++;
}
getch();
}

The loop in the above program is replacement of for loop. Here, the variable i is initialized before the while loop. The value of i is checked in condition. If the value of i is less than or equal to 10 then the loop will execute. During the execution, the current value of i is displayed and its value is incremented by 1. The condition is checked again for the new value of i. This process continues until the condition becomes false.

Ideally while loop is used in situation when the exact number of iterations is not known in advance. For example if we are required to keep accepting integer values from user and calculate their sum until sum is greater than 100. This can be done with while loop as explained in the following example.

Example 2: keep accepting integer values from user and calculate their sum until sum is greater than 100

#include “conio.h”
#include “iostream”
using namespace std;

void main()
{
int n,sum=0;
while (sum<=100)
{
cin>>n;
sum+=n;
}
cout<<“Sum of given values =”<<sum;
getch();
}

The output of the above program is as below.

113

Example3: keep accepting integer values from user and calculate their sum until 0 is entered

#include “conio.h”
#include “iostream”
using namespace std;

void main()
{
int n,sum=0;
cin>>n;
while (n!=0)
{
sum+=n;
cin>>n;
}
cout<<“Sum of given values =”<<sum;
getch();
}

The output of the above code is as below.

145

An important point to note in the last program is that we need to accept the number twice. Once before the while statement and then within the body of the loop to keep things going. It also shows that at least one value is required to be entered. If it is not 0 then another value will be entered until 0 is found. In this kind of situations, a do-while loop is preferred to be used.

do-while statement

As we have seen in the last example, that sometimes we know that the process will be performed at least once and can continue further. If this is the case then we use d-while loop. The syntax of do-while is as below.

do
{
statements;
.
.
}
while (condition);

As we can see that the body of the loop is executed first then the condition is checked which means that at any the body of the loop will be executed at least once. If the condition is true then it will repeat again until the condition becomes false as shown in the following example.

Example: keep accepting integer values from user and calculate their sum until 0 is entered

#include “conio.h”
#include “iostream”
using namespace std;

void main()
{
int n,sum=0;
do
{
cin>>n;
sum+=n;
}
while (n!=0);
cout<<“Sum of given values =”<<sum;
getch();
}

More Related Articles For You

    C++ Tutorial

    C++ Quizzes