foreach
Loop
One of the most common statements in D is the foreach
loop. It is used for applying the same operations to every element of a container (or a range).
Operations that are applied to elements of containers are very common in programming. We have seen in the for
Loop chapter that elements of an array are accessed in a for
loop by an index value that is incremented at each iteration:
for (int i = 0; i != array.length; ++i) { writeln(array[i]); }
The following steps are involved in iterating over all the elements:
- Defining a variable as a counter, which is conventionally named as
i
- Iterating the loop up to the value of the
.length
property of the array - Incrementing
i
- Accessing the element
foreach
has essentially the same behavior but it simplifies the code by handling those steps automatically:
foreach (element; array) {
writeln(element);
}
Part of the power of foreach
comes from the fact that it can be used the same way regardless of the type of the container. As we have seen in the previous chapter, one way of iterating over the values of an associative array in a for
loop is by first calling the array's .values
property:
auto values = aa.values; for (int i = 0; i != values.length; ++i) { writeln(values[i]); }
foreach
does not require anything special for associative arrays; it is used exactly the same as with arrays:
foreach (value; aa) {
writeln(value);
}
The foreach
syntax
foreach
consists of three sections:
foreach (names; container_or_range) {
operations
}
- container_or_range specifies where the elements are.
- operations specifies the operations to apply to each element.
- names specifies the name of the element and potentially other variables depending on the type of the container or the range. Although the choice of names is up to the programmer, the number of and the types of these names depend on the type of the container.
continue
and break
These keywords have the same meaning as they do for the for
loop: continue
moves to the next iteration before completing the rest of the operations for the current element, and break
terminates the loop altogether.
foreach
with arrays
When using foreach
with plain arrays and there is a single name specified in the names section, that name represents the value of the element at each iteration:
foreach (element; array) {
writeln(element);
}
When two names are specified in the names section, they represent an automatic counter and the value of the element, respectively:
foreach (i, element; array) { writeln(i, ": ", element); }
The counter is incremented automatically by foreach
. Although it can be named anything else, i
is a very common name for the automatic counter.
foreach
with strings and std.range.stride
Since strings are arrays of characters, foreach
works with strings the same way as it does with arrays: A single name refers to the character, two names refer to the counter and the character, respectively:
foreach (c; "hello") { writeln(c); } foreach (i, c; "hello") { writeln(i, ": ", c); }
However, being UTF code units, char
and wchar
iterate over UTF code units, not Unicode code points:
foreach (i, code; "abcçd") { writeln(i, ": ", code); }
The two UTF-8 code units that make up ç would be accessed as separate elements:
0: a 1: b 2: c 3: 4: � 5: d
One way of iterating over Unicode characters of strings in a foreach
loop is stride
from the std.range
module. stride
presents the string as a container that consists of Unicode characters. Its second parameter is the number of steps that it should take as it strides over the characters:
import std.range; // ... foreach (c; stride("abcçd", 1)) { writeln(c); }
Regardless of the character type of the string, stride
always presents its elements as Unicode characters:
a b c ç d
I will explain below why this loop could not include an automatic counter.
foreach
with associative arrays
When using foreach
with associative arrays, a single name refers to the value, while two names refer to the key and the value, respectively:
foreach (value; aa) { writeln(value); } foreach (key, value; aa) { writeln(key, ": ", value); }
Associative arrays can provide their keys and values as ranges as well. We will see ranges in a later chapter. .byKey
, .byValue
, and .byKeyValue
return efficient range objects that are useful in contexts other than foreach
loops as well.
.byValue
does not bring any benefit in foreach
loops over the regular value iteration above. On the other hand, .byKey
is the only efficient way of iterating over just the keys of an associative array:
foreach (key; aa.byKey) { writeln(key); }
.byKeyValue
provides each key-value element through a variable that is similar to a tuple. The key and the value are accessed separately through the .key
and .value
properties of that variable:
foreach (element; aa.byKeyValue) { writefln("The value for key %s is %s", element.key, element.value); }
foreach
with number ranges
We have seen number ranges before, in the Slices and Other Array Features chapter. It is possible to specify a number range in the container_or_range section:
foreach (number; 10..15) {
writeln(number);
}
Remember that 10 would be included in the range but 15 would not be.
foreach
with structs, classes, and ranges
foreach
can also be used with objects of user-defined types that define their own iteration in foreach
loops. Structs and classes provide support for foreach
iteration either by their opApply()
member functions, or by a set of range member functions. We will see these features in later chapters.
The counter is automatic only for arrays
The automatic counter is provided only when iterating over arrays. There are two options for other containers
- Taking advantage of
std.range.enumerate
as we will see later in theforeach
with Structs and Classes chapter. - Defining and incrementing a counter variable explicitly:
size_t i = 0; foreach (element; container) { // ... ++i; }
Such a variable is needed when counting a specific condition as well. For example, the following code counts only the values that are divisible by 10:
import std.stdio; void main() { auto numbers = [ 1, 0, 15, 10, 3, 5, 20, 30 ]; size_t count = 0; foreach (number; numbers) { if ((number % 10) == 0) { ++count; write(count); } else { write(' '); } writeln(": ", number); } }
The output:
: 1 1: 0 : 15 2: 10 : 3 : 5 3: 20 4: 30
The copy of the element, not the element itself
The foreach
loop normally provides a copy of the element, not the actual element that is stored in the container. This may be a cause of bugs.
To see an example of this, let's have a look at the following program that is trying to double the values of the elements of an array:
import std.stdio; void main() { double[] numbers = [ 1.2, 3.4, 5.6 ]; writefln("Before: %s", numbers); foreach (number; numbers) { number *= 2; } writefln("After : %s", numbers); }
The output of the program indicates that the assignment made to each element inside the foreach
body does not have any effect on the elements of the container:
Before: [1.2, 3.4, 5.6] After : [1.2, 3.4, 5.6]
That is because number
is not an actual element of the array, but a copy of each element. When the actual elements need to be operated on, the name must be defined as a reference of the actual element, by using the ref
keyword:
foreach (ref number; numbers) { number *= 2; }
The new output shows that the assignments now modify the actual elements of the array:
Before: [1.2, 3.4, 5.6] After : [2.4, 6.8, 11.2]
The ref
keyword makes number
an alias of the actual element at each iteration. As a result, the modifications through number
modify that actual element of the container.
The integrity of the container must be preserved
Although it is fine to modify the elements of a container through ref
variables, the structure of a container must not be changed during its iteration. For example, elements must not be removed nor added to the container during a foreach
loop.
Such modifications may confuse the inner workings of the loop iteration and result in incorrect program states.
foreach_reverse
to iterate in the reverse direction
foreach_reverse
works the same way as foreach
except it iterates in the reverse direction:
auto container = [ 1, 2, 3 ]; foreach_reverse (element; container) { writefln("%s ", element); }
The output:
3 2 1
The use of foreach_reverse
is not common because the range function retro()
achieves the same goal. We will see retro()
in a later chapter.
Exercise
We know that associative arrays provide a mapping from keys to values. This mapping is unidirectional: values are accessed by keys but not the other way around.
Assume that there is already the following associative array:
string[int] names = [ 1:"one", 7:"seven", 20:"twenty" ];
Use that associative array and a foreach
loop to fill another associative array named values
. The new associative array should provide values that correspond to names. For example, the following line should print 20:
writeln(values["twenty"]);