Introduction

C++ is a general-purpose programming language for game development, software engineering, data structures, browser development, and operating system development.
The C++ language introduced a new idea of the range-based for loop in C++11 and later versions, which is far superior to the regular For loop.
Let's first start our discussion with ‘For’ loop.
What is Range Based for Loop C++?
The range-based for loop in C++ is a convenient syntax for iterating over elements in a container, such as arrays, vectors, or other iterable objects. It simplifies the process of iterating through each element of the container by automatically handling the iteration logic and eliminating the need for explicit index manipulation or iterator declaration. This loop iterates over each element of the container sequentially, making it especially useful for iterating through the entire contents of a container without the complexities of traditional loop constructs.
The implementation of loop iteration in a range-based for loop does not always necessitate extensive coding. It is a consecutive iterator that iterates over each block element (from beginning to end).
Use the range-based for loop to create loops that must iterate through a range, defined as anything that can be repeated.
For example: std::vector or any other C++ Standard Library sequence whose range is determined by begin() and end() functions.
The name declared in the for-range declaration portion is unique to the “for statement” and cannot be displayed again in the expression or statement. The auto keyword is preferred in the for-range declaration portion of the statement.
Syntax:
for (range_declaration: range_expression )
loop block
In this syntax, there are three parameters:
🫳 range_declaration: It describes a variable with the same type as the collected elements represented by the range expression or a reference to that type.
🫳 range_expression: It describes an expression that represents the appropriate element sequence.
🫳 loop block: It describes the body of the range-based for loop, which includes one or more declarations that will be executed until the range expression is completed.