The for Loop
Part of TutorialFundamentals
Description
How to do the same thing over and over again.
Example
Most people don't like to do monotonous tasks over and over again (especially not a lazy programmer). That's where the for loop comes in. You could write a program like this:
writef("1\n"); writef("2\n"); writef("3\n"); writef("4\n"); ...
but that's no fun. The for statement allows the programmer step back and just say to the computer, "count from 1 to 10, and print each number."
D1.x
import std.stdio; int main() { for (int i = 1; i <= 10; i++) writefln(i); return 0; }
D2.x
import std.stdio; int main() { for (int i = 1; i <= 10; i++) writeln(i); return 0; }
Equivalent Example in QuickBASIC
DIM I% FOR I% = 1 TO 10 PRINT I% NEXT I%
Output
1 2 3 4 5 6 7 8 9 10
Source
Partially based on: The for Statement