The foreach statement lets you loop over every member of an array. The statement has two forms.
Simple form
foreach (value; array) statement; foreach (element_type value; array) statement;
This loops over the members of array. For each iteration, it copies the current member to value and then executes statement. For example:
int an_array[] = {1, 2} foreach (int num; an_array) { printf("%d", num); }
Enumerated form
The second form lets you specify an enumeration variable:
foreach (index, value; array) statement; foreach (int index; element_type value; array) statement;
For each iteration, this form assigns the current position in the array to index, copies the current member to value, and executes statement. For example:
string days[] = { "Mon", "Tue", "Wed", "Thu", "Fri" } foreach (int i; string name; days) { printf("Day number %d is %s", i, name); }
(This is similar to the common Python idiom for i, x in enumerate(xs):.)