Site icon Learn C++

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++:

[crayon-663c49dcecd95561598133/]

or with loop body:

[crayon-663c49dcecd9e495186483/]

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.

[crayon-663c49dcecda0930902961/]

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:

[crayon-663c49dcecda2942801752/]

here, we can use iterators as below,

[crayon-663c49dcecda4591364250/]

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

[crayon-663c49dcecda5635572472/]

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

[crayon-663c49dcecda7770633445/]

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.

[crayon-663c49dcecdab951917380/]

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.

[crayon-663c49dcecdad898165336/]

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.

[crayon-663c49dcecdaf465194104/]

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.

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.

Exit mobile version