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-6763f41508baa663295778/]

or with loop body:

[crayon-6763f41508bb1418971049/]

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-6763f41508bb2702627644/]

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-6763f41508bb3825825181/]

here, we can use iterators as below,

[crayon-6763f41508bb4298488557/]

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

[crayon-6763f41508bb5190488070/]

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

[crayon-6763f41508bb6228683402/]

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-6763f41508bb9124882091/]

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-6763f41508bba552246618/]

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-6763f41508bbb861137487/]

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