C++C++11C++14C++17C++20Introduction to C++IteratorsLearn C++Syntax

What is a range-based for loop in modern C++?

In C++, the for loops are one of the great features of C and C++ language that has many options to break, continue, or iterate in a loop. In modern C++, there is a range-based for loop that makes it simple to iterate through a variable type that has members (i.e. strings, lists, arrays, vectors, maps, etc.). The range-based for loop is a feature for the for() loops introduced by the C++11 standard and in this post, we explain what is range-based for loop in examples.

If you are new to programming and looking for a classic for() loop here is the post about it.

What is range-based for loop in modern C++?

In C++, for() function is used for loops, and they are one of the great features of C and C++ language and have many options to break or continue or iterate blocks of functionality. In modern C++, there is a range-based for loop that makes it simple to iterate trough a variable type that has members (i.e. strings, lists, arrays, vectors, maps, etc.).

The range-based for loop is a feature for the for() loops introduced by the C++11 standard. Range-based for loop is a feature for the for() loops introduced by the C++11 standard. In Clang-enhanced C++ compilers, you can create for loops that iterate through a list or an array without computing the beginning, the end, or using an iterator.

Here is the syntax for the range-based for loop in C++:

or with loop body:

Is there a simple array example with a range-based for loop in C++?

We can use range-based for loop to iterate through an array as below.

Is there a string example with range-based for loop in C++?

The following example shows how to use range-based for loop to iterate on the characters of a string:

here, we can use iterators as below,

or we can use as in classic for-loop as below,

or it can be used through the characters of a null-terminated character array,

Is there a UnicodeString example with range-based for loop in C++?

We can use range-based for loop to iterate UnicodeString or String in C++ Builder VCL or FMX applications as shown below.

Note that, in the newer version all String definitions are UnicodeString, that means you can use in String too.

Is there a vector example with a range-based for loop in C++?

Mostly range-based for loops are used to iterate through an vectors as below.

Is there a map example with a range-based for loop in C++?

Mostly range-based for loops are used to iterate through an vectors as below.

The range-based for loop is really useful to use in all ranges of data containers.

For more information on this feature and more details about ranges, see Ranges for the Standard Library.

What is a range based for loop in modern C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?