Home » Range-based for loop in C++

Range-based for loop in C++

by Online Tutorials Library

Range-based for loop in C++

In this topic, we will discuss the range-based for loop in the C++ programming language. The C++ language introduced a new concept of the range-based for loop in C++11 and later versions, which is much better than the regular For loop. A range-based for loop does not require large coding to implement for loop iteration. It is a sequential iterator that iterated each element of the container over a range (from beginning to end).

Range-based for loop in C++

Syntax

  1. range_declaration: It is used to declare a variable whose type is the same as the types of the collected elements represented by the range_expression or reference to that type.
  2. range_expression: It defines an expression that represents the suitable sequence of elements.
  3. loop statement: It defines the body of the range-based for loop that contains one or more statements to be repeatedly executed till the end of the range- expression.

Note: If we don’t know the data type of the container elements, we can use the auto keyword that automatically identifies the data type of the range_expression.

Program to print each element of the array using-range based for loop

Let’s consider an example to print the int and double array using the range-based for loop in C++.

program.cpp

Output

10 20 30 40 50  2.4 4.5 1.5 3.5 4.0  

Program to demonstrate the vector in range based for loop

Let’s write a simple program to implement the vector in range based for loop.

Program2.cpp

Output

5 10 25 20 25  

Program to print the arrays using Range based for loop in C++ with reference

Let’s consider an example to print the array elements using range based for loop in C++.

Program3.cpp

Output

Before updating the elements:  1 3 -2 4 6 7 9   After modification of the elements:  3 9 -6 12 18 21 27  

Nested range-based for loop

When a loop is defined inside the body of another loop, the loop is called a nested for loop. Similarly, when we define a range in a loop inside another range-based loop, the technique is known as a nested range-based for loop.

Syntax:

In the above syntax, we define one range-based for loop inside another loop. Here we call inner and outer range-based for loop in C++.

Program to print the nested range-based for loop in C++

Consider an example to demonstrate the nested range based for loop in C++ programming language.

Range.cpp

Output

x = 0 and j = 1   x = 0 and j = 2   x = 0 and j = 3   x = 0 and j = 4   x = 0 and j = 5   x = 1 and j = 1   x = 1 and j = 2   x = 1 and j = 3   x = 1 and j = 4   x = 1 and j = 5   x = 2 and j = 1   x = 2 and j = 2   x = 2 and j = 3   x = 2 and j = 4   x = 2 and j = 5   x = 3 and j = 1   x = 3 and j = 2   x = 3 and j = 3   x = 3 and j = 4   x = 3 and j = 5  

What is the difference between traditional for loop and range-based for loop?

A traditional for loop is used to repeatedly execute the block of code till the specified condition is true. A traditional for loop has three parameters, initialization of the variable, specify the condition, and the last one is counter that is incremented by one if the condition remains true.

Syntax:

Range-based loop

On the other hand, we have a new range-based for loop available in the C++ 11 and later version. It has two parameters, range declaration, and the range_ expression. It is also used to repeatedly execute the block of code over a range.

Syntax

The range_declaration is used to declare the type of variable related to the range_expression (container). The range_expression: It is just like a container that holds the same types of elements in a sequential manner. The loop_statement defines the statement which is executed inside for loop.

Advantages of the range-based for loop

  1. It is easy to use, and its syntax is also simple.
  2. A range-based for loop does not require the calculation of the number of elements in a containers
  3. It recognizes the starting and ending elements of the containers.
  4. We can easily modify the size and elements of the container.
  5. It does not create any copy of the elements.
  6. It is much faster than the traditional for loop.
  7. It usually uses the auto keyword to recognize the data type of the container elements.

Disadvantage of the range-based for loop

  1. It cannot traverse a part of a list.
  2. It cannot be used to traverse in reverse order
  3. It cannot be used in pointers.
  4. It does not offer to index of the current elements.

You may also like