void main()
{
import std.stdio : writeln;
int[] arr = [1, 3, 4];
int i = 0;
assert(arr.length > 0, "Array must contain at least one element");
do
{
arr[i++] *= 2;
} while (i < arr.length);
writeln(arr); // [2, 6, 8]
}
Foreach allows a less error-prone and better readable way to iterate collections. The attribute ref can be used if we want to directly modify the iterated element.
void main()
{
import std.stdio : writeln;
int[] arr = [1, 3, 4];
foreach (ref el; arr)
{
el *= 2;
}
...