# Range-based for loop in C++

C++Server Side ProgrammingProgramming

The range based for loop is added in C++ 11 standard and is a more compact form of its traditional equivalent. The range based for loop is used to iterate over elements of a container from beginning to end. The syntax for range-based for loop is as follows −

## Syntax

for( range-declaration : range-expression ) loop statement

range-declaration − it is declaration of a variable of type same as the type of elements of range-expression. Often the auto keyword is used to automatically identify the type of elements in range-expression.

range-expression − any expression used to represent a sequence of elements. Also Sequence of elements in braces can be used.

loop-statement − body of for loop that contains one or more statements that are to be executed repeatedly till the end of range-expression.

// Iterating over array
int arr[] = { 10,20,30,40,50 };
for (int num : arr)
printf("%d, ",num);
Is same as:
for ( int i=0;i<5;i++ )
printf("%d, ",arr[i]);

Here one can easily see that there is no need of calculating the size of array in rangebased for loop therefore no conditional expression is required. Also, there is no requirement of increment or decrement operation. The num in range based for loop above for each iteration takes the value of element from array arr[] from beginning till end. No elements are skipped until one of the jump statements are executed.

Break − terminates the loop for all rest of the iterations.

Continue − skips the current iteration and moves to next

goto − jumps out of the loop to statement followed by label −

• Easy to use and simple syntax.

• No need to calculate the number of elements in container or size of range-expression.

• If data type of range-declaration is not known then auto specifier can be used in its place, that automatically makes it compatible with range-expression’s type.

• No conditional statements or increment/decrement statements are required.

• Best in case where the whole container is to be iterated in one go.

• Iterates over every element between begin() and end(). Specific indexes cannot be treated.

• Revisiting one or more elements and skipping a group of elements cannot be done using range-based for loop.

• The array cannot be iterated in reverse order. For that <boost/range/adaptor/reversed.hpp> library is used

## Example

Live Demo

#include <iostream>
#include <vector>
#include <map>
int main(){
int arr[] = { 10,20,30,40,50 };
for ( int i=0;i<5;i++ )
printf("%d, ",arr[i]);
printf("\n");
for (int num : arr)
printf("%d, ",num);
printf("\n");
// for character array
char str[] = "Hello World";
for (char c : str)
printf("%c ",c);
printf("\n");
for (char c : "Hello World")
printf("%c ",c);
printf("\n");
std::map <int, char> MAP({{1, 'A'}, {2, 'B'}, {3, 'C'}});
for (auto m : MAP)
printf("{ %d, %c }", m.first,m.second);
}

## Output

10, 20, 30, 40, 50,
10, 20, 30, 40, 50,
H e l l o W o r l d
H e l l o W o r l d
{ 1, A }{ 2, B }{ 3, C }
Published on 28-Jul-2020 10:29:48