VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-loop-over-an-array-in-cpp/

⇱ How to Loop Over an Array in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Loop Over an Array in C++?

Last Updated : 23 Jul, 2025

In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++.

Example:

Input:
int arr[] = [1, 2, 3, 4, 5]

Output:
Array Elements: 1 2 3 4 5

Loop Through Each Item of an Array in C++

For iterating through an , we create a loop that iterates the same number of times as there are elements in the array. For that, we will create a loop variable that starts from 0 (as arrays in C++ are 0-indexed), increments by one in each iteration and goes till it is less than the size of the array.

We can use any loop of our choice. Here, we are using for loop

Syntax for Iterating Through an Array in C++

Below is the syntax of a basic for loop to traverse over an array.

for (lv; lv < size_of_array; lv++) {
 //body of loop
}

where,

  • lv: It represents the loop variable.

C++ Program to Loop Over an Array

The below example demonstrates how we can loop over an array in C++.


Output
Elements in an Array: 1 2 3 4 5 

Time Complexity: O(n), here n is a size of an array.
Auxiliary Space: O(1)

Note: Besides using traditional loops, we can also use the and algorithm to loop over an array and access each element.

Comment
Article Tags: