In this C++ tutorial, you will learn how to iterate over array and traverse the elements in array using loop statements like While loop, For loop, or Foreach loop, with examples.

Loop through Array in C++

You can loop through array elements using looping statements like while loop, for loop, or for-each statement.

For a built-in C++ array, you can either access each element by its index or use a range-based for loop to work directly with the elements. An index-based loop is useful when you need the position of an element. A range-based loop is usually simpler when you only need each value.

1. Iterate over array using While loop

In this example, we will use C++ While Loop to iterate through array elements.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int arr[7] = {25, 63, 74, 69, 81, 65, 68};
   
   int i=0;
   while (i < 7) {
      cout << arr[i] << "  ";
       i++;
   }
}

Output

25  63  74  69  81  65  68

The variable i starts at 0, which is the index of the first element. The condition i < 7 keeps the loop within the valid indexes 0 through 6. After printing an element, i++ moves to the next index.

2. Iterate over array using For loop

In this example, we will use C++ For Loop to iterate through array elements.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int arr[7] = {25, 63, 74, 69, 81, 65, 68};
   
   for (int i=0; i < 7; i++) {
      cout << arr[i] << "  ";
   }
}

Output

25  63  74  69  81  65  68

A for loop keeps the initialization, condition, and index update in one place. This form is useful when you need the current array index as well as the element value.

3. Iterate over array using ForEach statement

In this example, we will use C++ Foreach statement to iterate through array elements.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int arr[7] = {25, 63, 74, 69, 81, 65, 68};
   
   for (int element: arr) {
      cout << element << "  ";
   }
}

Output

25  63  74  69  81  65  68

This is C++’s range-based for loop. It visits every element in the array without requiring an explicit index or array length. In the example, element receives a copy of each integer in turn.

Loop through a C++ array without hard-coding its length

The earlier index-based examples use the literal value 7 because the array contains seven elements. In reusable code, it is better to derive the number of elements from the array instead of repeating its size manually.

With C++17 or later, std::size() can return the number of elements in a built-in array.

</>
Copy
#include <iostream>
#include <iterator>
using namespace std;

int main() {
    int arr[] = {25, 63, 74, 69, 81, 65, 68};

    for (size_t i = 0; i < std::size(arr); ++i) {
        cout << arr[i] << "  ";
    }
}

Output

25  63  74  69  81  65  68

If the number of elements is not known when you write the source code but the object is still a built-in array, deriving the length this way avoids a separate hard-coded loop bound. A range-based for loop is even simpler when you do not need an index.

Find C++ array length with sizeof before iterating

Another common technique for a built-in array is to divide the total size of the array by the size of one element.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30, 40};
    size_t length = sizeof(arr) / sizeof(arr[0]);

    for (size_t i = 0; i < length; ++i) {
        cout << arr[i] << " ";
    }
}

Output

10 20 30 40

This calculation works while arr is an actual array in the current scope. It should not be used on a pointer that merely points to the first element of an array, because sizeof would then return the size of the pointer rather than the number of array elements.

Iterate over a C++ array when its size is passed to a function

A built-in array normally loses its size information when it is passed to a function parameter written as a pointer. If a function receives a pointer to the first element, pass the number of elements separately so that the loop knows where to stop.

</>
Copy
#include <iostream>
using namespace std;

void printArray(const int arr[], size_t length) {
    for (size_t i = 0; i < length; ++i) {
        cout << arr[i] << " ";
    }
}

int main() {
    int arr[] = {5, 10, 15, 20};
    size_t length = sizeof(arr) / sizeof(arr[0]);

    printArray(arr, length);
}

Output

5 10 15 20

A raw pointer alone does not tell a loop how many valid elements follow it. The program needs the length from another source, such as a separate size argument, an iterator pair, or a container that stores its own size.

Modify C++ array elements while looping

When you want a range-based loop to change the original array, declare the loop variable as a reference. Without the ampersand, the loop variable is a copy and changes to it do not affect the array.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4};

    for (int& element : arr) {
        element *= 10;
    }

    for (int element : arr) {
        cout << element << " ";
    }
}

Output

10 20 30 40

Here, int& element refers directly to each array element. Therefore, multiplying element by 10 changes the value stored in the array.

Read C++ array elements with a const reference

For an array of larger objects, a const reference lets you read each element without making a copy and prevents accidental modification through the loop variable.

</>
Copy
for (const auto& element : arr) {
    // read element without copying it
}

For small fundamental values such as int or char, iterating by value is also straightforward. References become especially useful when the elements are objects for which copying is unnecessary.

Loop through a two-dimensional C++ array

A two-dimensional array can be traversed with nested loops. The outer loop visits each row, and the inner loop visits each element within that row.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int arr[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    for (const auto& row : arr) {
        for (int element : row) {
            cout << element << " ";
        }
        cout << '\n';
    }
}

Output

1 2 3
4 5 6

The row variable is declared as const auto& so that each row remains an array instead of being copied or converted to a pointer.

Print a C++ array without writing an explicit loop

Writing cout << arr does not print every element of a normal numeric array. To display all values, some form of iteration is required. You can write the loop directly, as in the examples above, or use a standard library algorithm that performs the iteration for you.

</>
Copy
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;

int main() {
    int arr[] = {10, 20, 30};

    for_each(begin(arr), end(arr), [](int value) {
        cout << value << " ";
    });
}

Output

10 20 30

This example does not contain an explicit loop statement in main(), but std::for_each still iterates through the range internally.

Choose the right loop for a C++ array

  • Range-based for loop: use it when you want to process every element and do not need its index.
  • For loop with an index: use it when the element position is part of the operation or when you need controlled stepping.
  • While loop: use it when loop progression depends on logic that is clearer outside the compact for syntax.
  • Reference in a range-based loop: use auto& or an explicit reference type when the original array elements must be modified.
  • Const reference: use const auto& when reading larger elements without copying them.

Common errors when iterating over a C++ array

  • Going past the last index: an array with n elements has valid indexes from 0 through n - 1. Accessing outside those bounds results in undefined behavior.
  • Hard-coding the wrong length: prefer a derived length such as std::size(arr) when practical.
  • Using sizeof on a pointer: after an array has decayed to a pointer, sizeof(pointer) / sizeof(pointer[0]) does not recover the original number of elements.
  • Expecting range-based iteration to provide an index: it provides each element directly. Use an index-based loop when you need the position.
  • Changing a copied loop variable: use a reference if modifications must be written back to the original array.

C++ array iteration summary

A C++ array can be traversed with a while loop, an index-based for loop, or a range-based for loop. Use an index when you need element positions, and use a range-based loop when you only need the elements. Avoid relying on a hard-coded array length when it can be derived safely, and remember that a raw pointer by itself does not carry the number of array elements.

In this C++ Tutorial, we learned how to iterate through elements of Array using looping statements.