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.
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
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
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
Conclusion
In this C++ Tutorial, we learned how to iterate through elements of Array using looping statements.