C++ Arrays and Loops

Loop Through an Array

You can loop through the array elements with the for loop.

The following example outputs all elements in the cars array:

Example

// Create an array of strings
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};

// Loop through strings
for (int i = 0; i < 5; i++) {
  cout << cars[i] << "\n";
}

This example outputs the index of each element together with its value: Continue reading C++ Arrays and Loops