Range-based for loop in C++21 Mar 2025 | 5 min read 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). ![]() Syntax
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 loopLet'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 loopLet'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 referenceLet'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 loopWhen 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
Disadvantage of the range-based for loop
Next TopicImplement-rand10-using-rand7-in-cpp |
In this article, we will discuss how to generate random double numbers in C++. In C++, the header offers many random number-generating functions that can be used to generate random double numbers. The std::random_device class, which functions as a seed generator, and the std::mt19937 class, which is...
4 min read
Bipartite Graph Definition Bipartite graphs hold significant importance in various domains due to their distinct properties and applications in practical problem-solving scenarios. Here's an exploration of their key properties, applications, and their implications in different fields: Properties of Bipartite Graphs 2-Colorable: A fundamental property of bipartite graphs is their...
15 min read
A 21-sided polygonal number is called an isosikaienneagonal number. According to the formula P21 (k) = k.(19k−17)/2, where k is the location of the sequence. 1, 21, 62, 124, etc., are the numbers in that order. A C++ implementation of the concept would be a...
4 min read
Introduction As it pertains to programming in C++, the Standard Template Library (STL) has various features meant to assist in the manipulation of complex numbers and their relations. Of these subtitles, the std::polar function stands out as one of the most helpful since it is designed to...
10 min read
The Juggling Algorithm is a useful C++ technique that does rotations by shifting the elements around. It divides the array into sets using the Greatest Common Divisor (GCD) of the array size n and number of spots d to be rotated. After that, the elements are...
5 min read
C++ is an object-oriented programming language that values data and how it is managed. It encourages modular programming by allowing developers to partition memory into different parts for data and functions. Both structures and classes in C++ allow for the creation of custom data types...
7 min read
The "Trapping Rainwater" problem is a well-known computational challenge that demonstrates the application of algorithmic thinking to solve real-world problems. It requires analyzing an array of integers, representing elevations, to determine the amount of water that can be trapped between the bars after rainfall. This...
11 min read
Difference between std:: and std::advance in C++ In this article, we will discuss the difference between std:: and std::advance method in C++. Before discussing their differences, we must know about the std:: and std::advance with their syntax, functionality, and example. What is the std:: function in C++? The <iterator>...
4 min read
In this article, we will discuss how to call a virtual function from a derived class in C++ with their advantages. Introduction: Polymorphism is one of the main features in object-oriented programming, especially in C++. In other words, it refers to the occurrence of many forms. These different...
7 min read
In this article, we will discuss the Program to print Crown Patterns in C++. Before discussing the Program to print a Crown Pattern in C++, we must know about the steps to print a Crown Pattern in C++ example. What is the Crown Pattern? A common design...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India