Difference between revisions of "Looping over integers"

From D Wiki
Jump to: navigation, search
(Created page with "Looping over a range of consecutive integers is a very common programming task. In the family of C-like languages (C, C++, Java, etc.), the usual way to write this is somethin...")
(No difference)

Revision as of 18:57, 14 December 2012

Looping over a range of consecutive integers is a very common programming task. In the family of C-like languages (C, C++, Java, etc.), the usual way to write this is something like:

int i;
for (i=0; i < max; i++)
{
    // loop body here
}

D also supports exactly the same syntax, but its much more powerful foreach construct coupled with its slicing syntax allows you to write such loops in a much more readable way:

foreach (i; 0 .. max)
{
    // loop body here
}