DLang Notes and Examples
← prev | next →     Top-level ToC     /control-structures.html     (printable version)

1 Looping

auto arr = [1, 2, 3, 4];
foreach (x; arr) {
    writeln(x);
    // `x` is a local that's assigned to each value of arr.
}

foreach (ref x; arr) {
    // assigning to `x` changes `arr`
}

foreach (i, elem; arr) {
    // `i` is the idx of `elem` in `arr`.
}

In foreach (elem, arr) {...}, assigning to elem doesn’t affect arr.