for loop c++ array code example
Example 1: lopping over an array c++
for (int i = 0; i < arr.size(); ++i){
cout << i << ":\t" << arr[i] << endl;
}
for (int element : arr){
cout << element << endl;
}
for (int &element : arr){
cout << element << endl;
}
Example 2: c++ loop through int array
#include <iostream>
#include <array>
int main()
{
int aNumbers[] = { 0, 1, 2, 3, 4, 5 };
int count = 0;
for (int aNumber : aNumbers)
{
std::cout << "Element "<< count << " : " << aNumber << std::endl;
count++;
}
}
Example 3: loop through array c++
for(int i = 0; i < 4; i++) {
cout << cars[i] << "\n";
}
Example 4: for loop with array c++
int v[] = {1,2,3,4,5};
for (int n : v)
cout << n << endl;