foreach Loop Properties

This lesson explains the different properties of foreach loop.

Counter for containers #

The automatic counter in foreach is provided only when iterating over arrays. There are two options for other containers:

  • Taking advantage of std.range.enumerate

  • Defining and incrementing a counter variable explicitly:

size_t i = 0;

foreach (element; container) {
    // ...
    ++i; 
}

A counter variable is needed when counting a ...

Access this course and 1400+ top-rated courses and projects.