C++ Array Loop

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

C++ Array – Iterate using While Loop

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

C++ Program

#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

C++ Array – Iterate using For Loop

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

C++ Program

#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

C++ Array – Iterate using ForEach Statement

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

C++ Program

#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

Conclusion

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