c++ loop through array
To loop through an array in C++, you can use a for loop. Here's an example of how to do this:
#include <iostream>
int main() {
int arr[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
std::cout << "Element at index " << i << ": " << arr[i] << std::endl;
}
return 0;
}
In this example, we first declare an array arr
with some integer values. Then, we use a for loop to iterate through the array. The loop initializes an integer variable i
to 0. It then checks if i
is less than 5, which is the size of the array. If the condition is true, the loop body is executed, which prints the element at index i
of the array. After each iteration, the value of i
is incremented by 1. This process continues until i
is no longer less than 5.