In this C++ tutorial, you will learn how to print an array (the elements in the array) using loop statements like While loop, For loop, or Foreach loop, with examples.
C++ Print Array
You can print array elements in C++ using looping statements or foreach statement.
1. Print array using While loop
In this example, we will use C++ While Loop to print 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. Print array using For loop
In this example, we will use C++ For Loop to print 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. Print array using ForEach statement
In this example, we will use C++ Foreach statement to print 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 print array elements using looping statements.