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).
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++.
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.
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++.
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.
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.
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.
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.
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
Disadvantage of the range-based for loop