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.
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.
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.
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.
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
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- Storage Classes in C++
- Functions in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++
C++ Tutorial
C++ Quizzes