VOOZH about

URL: https://www.geeksforgeeks.org/cpp/range-based-loop-c/

⇱ Range-Based for Loop in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Range-Based for Loop in C++

Last Updated : 16 Jun, 2026

The range-based for loop, introduced in C++11, provides a simple and readable way to iterate over all elements of a range. It automatically traverses arrays, strings, and STL containers without requiring explicit indices or iterators.

  • Simplifies iteration over arrays, strings, and containers.
  • Eliminates the need to manage loop counters and iterators manually.
  • Improves code readability and reduces the chances of iteration-related errors.

Output
1 2 3 4 5 

Explanation: The range-based for loop automatically visits each element of the vector. There is no need to specify the container size or use iterators explicitly.

Syntax

where,

  • declaration: Variable used to represent each element of the range.
  • range: The array, string, container, or any iterable object being traversed.

Working of Range-Based for Loop

The working of a range-based for loop is as follows:

  1. The loop obtains the beginning and ending positions of the specified range.
  2. The first element is assigned to the loop variable.
  3. The loop body is executed using that element.
  4. The loop variable is updated with the next element in the range.
  5. Steps 3 and 4 repeat until all elements have been processed.
  6. Control exits the loop after the last element is visited.

Examples of Range Based for Loop

The below example demonstrates the use of range based for loop in our C++ programs:

Iterate over an Array

The following program prints all elements of an array using a range-based for loop.


Output
1 2 3 4 5 

Explanation: Each element of the array is accessed sequentially and assigned to the loop variable x.

Iterate over a Map

Since each element of a map is stored as a key-value pair, the loop variable must represent a pair object. Using auto allows the compiler to determine the appropriate type automatically.


Output
1: A
2: B
3: C
4: D
5: E

Structured Bindings with Maps (C++17)

Since C++17, structured bindings can be used to directly access keys and values without referring to .first and .second.


Output

1: A
2: B
3: C
4: D
5: E

Explanation: Structured bindings unpack each key-value pair into separate variables, making the code more readable.

Iterate by Reference

By default, the loop variable stores a copy of each element. To modify the original elements of the container, the loop variable must be declared as a reference.


Output
2 3 4 5 6 

Explanation: Since x is declared as a reference (auto&), modifications are applied directly to the elements stored in the vector.

Advantages of Range-Based for Loop

The range-based for loop offers several benefits over traditional iteration methods, making code simpler, safer, and easier to read.

  • Provides cleaner and more readable syntax.
  • Eliminates the need for explicit loop counters and iterators.
  • Reduces the possibility of index-related errors.
  • Works seamlessly with arrays, strings, and STL containers.
  • Makes code easier to maintain and understand.
Comment
Article Tags:
Article Tags: