Loops in C++ | Different types of Loops in C++

by Aug 16, 2021C++, Coding0 comments

In many situations, we need to repeat the block of code a specific number of times or until the given condition is satisfied. A type of control structure that repeats a statement or set of statements is known as iterative, repetitive, or looping structure. There are multiple types of loops in C++. In looping structure, the sequence of statements to be executed is kept in the curly brackets and called the loop’s body.

In sequential structure, loops are executed only once, while control structure may execute a statement or skip any statement based on any condition.

There are three different types of loops available in C++.

  • While loop.
  • Do while loop.
  • For loop.

Loops are used for two different purposes.

  • To execute a statement or set of statements a specified number of times. For example, you want to display any message on the screen ten times.
  • To use a sequence of values. For example, you want to display the prime numbers from 1 to 100.

Counter control loops.

The counter control loops depend on the value of a variable called the counter variable. The value of the counter variable is incremented or decremented whenever the loop body is executed.

The loop terminates when the given condition meets. The number of the iterations of the counter variable depends on the following.

  • The initial value of the counter variable.
  • The condition to terminate the loop.
  • The increment or decrement value.

Sentinel control loop.

This type of loop depends on the sentinel value. Sentinel control loops are also called conditional loops. The loop terminates when the sentinel value is encountered. 

The number of iterations is not known in this type of loop. It depends on the input from the user or a file. For example, a loop may execute while the user-entered value is not 2. In this example, 2 is the sentinel value. If the user entered 2 in the first attempt, the loop terminates immediately in the first attempt.

The sentinel value is primarily used in while and do-while loops, but we can also use the sentinel values in for loops.

Categories of loops.

Depends on the working loops are divided into two different categories. Entry control loop and exit control loop.

Entry control loops.

In entry control loops, the test condition is executed before the execution of the loop body. If the test condition returns true, then the loop body is executed. Both the while and for loop are entry control loops.

Exit control loop.

In the exit control loop, the loop body is executed first. Then the test condition is evaluated if the test condition returns true, then the loop body is again considered, but if it returns false, then the loop terminates.

In the exit control loop, the loop body is executed at least once irrespective of the test condition is true or false. The do-while loop is the exit control loop.

Working of looping structure in C++.

Working of looping structure in C++.

Loops in C++ While loop.

The simplest loop of the C++ programming language is the while loop. While loop executes a statement or set of statements while the given condition remains true.


We mainly use while loop when we don’t know the exact number of iterations.

Flow chart of while loop.

Working of looping structure in C++.

Syntax of while loop.

The syntax of the while loop is as follow.

// syntax of the while loop in C++

while (condition)
{
	statements;
}

Purpose of conditions in while loop.

It is a relational expression. The loop’s body is only executed if the condition is true. Otherwise, the loop will terminate.

What are statements in while loop?

The statements are also called the body of the loop. This part is only executed if the condition is true. More than one statement is written in curly braces.

Working of while loop.

First of all, the condition is evaluated. If the condition part returns true, then the body of the loop is evaluated. And if the condition returns false, the body of the loop will be terminated. And the loop iterates an infinite number of times if the loop’s condition remains true.

C++ Examples of while loop.

Write a C++ program that displays all the characters from a character pointer.

//C++ program that displays all the characters from a character pointer.
#include<iostream>
using namespace std;
int main()
{
	char *ptr = { "codefossils.com" };// character pointer 
	int i = 0;
	cout << " required daa is : " << endl;
	while (ptr[i] != '\0')// the while loop runs untill it receives a terminating character at the end of pointer
	{
		cout << ptr[i];
		i++;
	}
	cout << endl;
	
	return 0;
}

Output:

output of while loop example

Loops in C++ Do while loop.

The do while loop is also an iterative control loop. This loop also executes one or more statements based on the given condition.

The difference between the while and do while loop is that in the do while loop, the loop’s body executes at least once, even the condition is false in the first attempt.


In the do-while loop, the body comes first, and the condition part comes after the loop’s body. This loop is mainly used when it is must to execute the loop’s body at least once.

The flow chart of do while loop is as follow.

flow chart of do while loop

Syntax of do while loop.

The syntax of the do-while loop is as follows.

// syntax of do while loop
do// it indicates the starting of the do while loop
{
	statement1;
	statement2;
	statement3;


	statementN;
} while (condition);// terminating condition 

What is the Do keyword stands for in the do-while loop?

It is the keyword that indicates the beginning of the do-while loop.

What are statements in do while loop?

Statements are the loop’s body, which is executed at least once even if the condition is wrong. More than one statement is written in curly braces ({}).

What is the purpose of Conditions in the do-while loop?

It is given as a relational expression. The statements are executed based on the given condition. If the condition returns false, the body of the loop executes only once.

Working of do while loop.

First of all, the body of the loop is executed. After executing all the statements in the body, the condition is evaluated. If the condition returns true, then the loop’s body is re-evaluated as long as the condition returns true.

The loop terminates when the condition returns false. The loop body executes at least once, even if the condition returns false in the first attempt.

C++ example of do while loop.

Write a program that gets the starting and ending point from the user and displays all the odd numbers in the given range using the do while loop.

// C++ example of do while loop
#include<iostream>
using namespace std;
int main()
{
	int start = 0, end = 0, temp = 0;
	cout << " Enter starting number : ", cin >> start;// get starting number from user
	cout << " Enter ending number : ", cin >> end;// get ending number from user
	temp = start;// temp variable the starting value
	do// indicates the starting of do while loop
	{
		if (temp % 2 != 0)// check that the given word is odd or not
			cout << temp << endl;// print if the number is odd
		temp++;
	} while (temp <= end);// condition to  terminate loop when temp becomes greater tahn ending value
	system("pause");
	return 0;
}

output of example of do while loop in cpp

Loops in C++ For loop.

For loop is an example of the counter control loop. To know more about for loop in C++ with detailed explanations and examples, please click here.

Loops in C++ Nested loop.

A loop within a loop is called a nested loop. In the nested loop, the inner loop is executed entirely with each change in the counter variable’s value of the outer loop.


We can do nesting up to any level. The complexity of the nested loop with the increase in the level of nesting. We can use any loop as the inner loop of another loop.

For example, do while loop can be used as an outer loop and while loop can be used as an inner loop, etc.

Syntax of nested loop.

The syntax of the nested loop is as follow.

// syntax of nested loop
while (condition)
{
	for (initialization; condition; increment / decrement)
	{
		statements;
	}
}

Example of nested loop in C++ is as follow:

// example of nested for loop in C++
#include<iostream>
using namespace std;
int main()
{
	for (int i = 0; i < 5; i++)//  outer loop
	{
		for (int j = 0; j < 5; j++)// inner loop
			cout << " i " << i << " j " << j << endl;// statement
		cout << endl;// statement
	}
	system("pause");
	return 0;
}

Output:

output of nested for loop example

Frequently Asked Question’s

How many types of loops are in C++?

There are four different types of loops in C++.

  1. For loop.
  2. While loop.
  3. Do while loop.
  4. Nested loop.

What are counter Control loops?

The counter control loops depend on the value of a variable called the counter variable. The value of the counter variable is incremented or decremented whenever the loop body is executed. The loop terminates when the given condition meets.

What are sentinal control loops?

This type of loop depends on the sentinel value. Sentinel control loops are also called conditional loops. The loop terminates when the sentinel value is encountered. 

The number of iterations is not known in this type of loop. It depends on the input from the user or a file.

What are entry control loops?

In entry control loops, the test condition is executed before the execution of the loop body. If the test condition returns true, then the loop body is executed. Both the while and for loop are entry control loops.

What are exit control loops?

In the exit control loop, the loop body is executed first. Then the test condition is evaluated if the test condition returns true, then the loop body is again considered, but if it returns false, then the loop terminates.

In the exit control loop, the loop body is executed at least once irrespective of the test condition is true or false. The do-while loop is the exit control loop.

What is while loop in C++?

The simplest loop of the C++ programming language is the while loop. While loop executes a statement or set of statements while the given condition remains true.

If you find it helpful then don't forget to Share it :

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Latest Posts

More From The Blog