C++ Program to Iterate Over an Array


Arrays are data of the same type stored in contiguous locations in memory. To access or address an array, we use the starting address of the array. Arrays have indexing, using which we can access the elements of the array. In this article, we take a look at methods to iterate over an array. This means accessing the elements that are present in an array.

Using for loop

The most common method of iterating over an array is using for loops. We use a for loop to iterate through an array in the next example. One thing is to be noted, we need the size of the array in this.

Syntax

for ( init; condition; increment ) {
   statement(s);
}

Algorithm

  • Take input in an array arr of size n.
  • For i := 0 to i := n, do:
    • Print (arr[i])

Example

#include <iostream>
#include <set>
using namespace std;

// displays elements of an array using for loop
void solve(int arr[], int n){
   for(int i = 0; i < n; i++) {
      cout << arr[i] << ' ';
   }
   cout << endl;
}
int main(){
   int arr[] = {10, 5, 11, 13, 14, 2, 7, 65, 98, 23, 45, 32, 40, 88, 32};
   int n = 15;
   cout << "Values in the array are: ";
   solve(arr, n);
   return 0;
}

Output

Values in the array are: 10 5 11 13 14 2 7 65 98 23 45 32 40 88 32

Using while loop

Similar to for loop, we can use while loops to iterate through an array. Also in this case the size of the array has to be known or determined.

Syntax

while(condition) {
   statement(s);
}

Algorithm

  • Take input in an array arr of size n.
  • i := 0
  • while i < n, do:
    • Print (arr[i])
    • i := i + 1

Example

#include <iostream>
#include <set>
using namespace std;

// displays elements of an array using for loop
void solve(int arr[], int n){
   int i = 0;
   while (i < n) {
      cout << arr[i] << ' ';
      i++;
   }
   cout << endl;
}
int main(){
   int arr[] = {10, 5, 11, 13, 14, 2, 7, 65, 98, 23, 45, 32, 40, 88, 32};
   int n = 15;
   cout << "Values in the array are: ";
   solve(arr, n);
   return 0;
}

Output

Values in the array are: 10 5 11 13 14 2 7 65 98 23 45 32 40 88 32

Using forEach loop

We can also use the modern for-each loop to iterate through the elements in the array. The major plus side of this method is we do not need to know the size of the array.

Syntax

for (datatype val : array_name) {
   statements
}

Algorithm

  • Take input in an array arr of size n.
  • for each element val in arr, do:
    • print(val)

Example

#include <iostream>
#include <set>
using namespace std;
int main(){
   int arr[] = {10, 5, 11, 13, 14, 2, 7, 65, 98, 23, 45, 32, 40, 88, 32};
   
   //using for each loop
   cout << "Values in the array are: ";
   for(int val : arr) {
      cout << val << ' ';
   }
   cout << endl;
   return 0;
}

Output

Values in the array are: 10 5 11 13 14 2 7 65 98 23 45 32 40 88 32 

Conclusion

Various ways of iterating through an array in C++ are described in this article. The main drawback of the first two methods is that the size of the array has to be known beforehand, but if we use the for-each loop, that problem can be mitigated. for-each loops support all STL containers and are easier to use.

Updated on: 14-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements