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 the with its mathematical properties, Recursive and optimized technique, and an example. What is the ? The Perrin sequence is an integer sequence that follows a specific recurrence relation. It is defined as follows: the first three terms are 3,...
8 min read
In this article, we discuss about . The Segmented Sieve is an optimized version of the Normal Sieve Algorithm. Unlike the normal Sieve, which computes all the multiples of every such number, the Segmented Sieve computes multiples of only some of the primes so calculated...
6 min read
Difference between C++ and Python C++ and Python are both popular programming languages. Python is a popular, high-level, and object-oriented programming language. On the other hand, C++ is a general-purpose programming language. In this article, we will discuss the difference between C++ and Python. Before discussing the...
6 min read
Prime numbers play a central role in various fields, from number theory and cryptography to computer science and engineering. Efficiently generating prime numbers up to a given limit is a classic problem that has been tackled using different algorithms. Among these, the Sieve of Sundaram...
13 min read
Introduction C++ is a powerful programming language for its rich standard library that consists of various functions and utilities to help math computations. Special mathematical functions are among these utilities, which include Hermite Polynomials. Hermite polynomials are important in quantum mechanics, probability theory, and numerical analysis fields....
7 min read
Introduction: Delaunay triangulation stands as a bedrock concept in computational geometry. It is widely applied for computer graphics, meshing, terrain modelling, and others. It was named after Boris Delaunay, who first described it in 1934. After that, it has been highly appreciated because of its efficiency and...
12 min read
In this article, we will examine a few cousin prime pairs and construct an algorithm that can produce such unique pairs. An example in C++ will be provided to demonstrate efficient methods of finding and printing cousin prime pairs. Cousin Prime and the Prime Number Concept Prime...
4 min read
The FizzBuzz problem is one of the classic coding challenges frequently utilized to screen a programmer for general knowledge of programming languages, control structures, and problem-solving abilities in technical interviews. Although it may seem simple, it would demonstrate whether we know the basics, including loops, conditionals,...
6 min read
In this article, we will discuss the Lobb Number with its different methods, examples, time complexity, and space complexity. Lobb Number A valid series of balanced parentheses can be formed by arranging n+m open parentheses in a certain fashion. This is known as the Lobb number Lm,n...
4 min read
Optimization problems are ubiquitous in the fields of science, engineering, and technology. From designing efficient circuits to planning delivery routes, optimizing solutions is a fundamental task that requires powerful algorithms. However, many real-world optimization problems are non-linear, complex, and riddled with local optima, making it...
13 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
