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()/*from www. j  a  va  2  s .  c o  m*/
{
  int production[] = { 1, 6, 8, 4, 3 };

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

Result