Cpp - for each loop

Introduction

for loop can loop through every element in an array.

The foreach has two sections separated by a colon : instead of three separated by semicolons.

The first section is a variable that will hold an element of the array. The second is the name of the array.

Demo

#include <iostream> 

int main()/*w  w  w . j av  a 2s .  co m*/
{
  int production[] = { 1, 6, 8, 4, 3 };

  for (int year : production)
  {
    std::cout << "Output: " << year << std::endl;
  }
}

Result