| 1 | = The ''for'' Loop = |
---|
| 2 | |
---|
| 3 | ''Part of'' TutorialFundamentals |
---|
| 4 | |
---|
| 5 | == Description == |
---|
| 6 | |
---|
| 7 | How to do the same thing over and over again. |
---|
| 8 | |
---|
| 9 | == Example == |
---|
| 10 | |
---|
| 11 | 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: |
---|
| 12 | {{{ |
---|
| 13 | write("1\n"); |
---|
| 14 | write("2\n"); |
---|
| 15 | write("3\n"); |
---|
| 16 | write("4\n"); |
---|
| 17 | ... |
---|
| 18 | }}} |
---|
| 19 | 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." |
---|
| 20 | |
---|
| 21 | ==== Example Code ==== |
---|
| 22 | {{{ |
---|
| 23 | #!d |
---|
| 24 | import std.stdio; |
---|
| 25 | |
---|
| 26 | void main() |
---|
| 27 | { |
---|
| 28 | for (int i = 1; i <= 10; i++) |
---|
| 29 | writeln(i); |
---|
| 30 | } |
---|
| 31 | }}} |
---|
| 32 | |
---|
| 33 | == Output == |
---|
| 34 | |
---|
| 35 | {{{ |
---|
| 36 | 1 |
---|
| 37 | 2 |
---|
| 38 | 3 |
---|
| 39 | 4 |
---|
| 40 | 5 |
---|
| 41 | 6 |
---|
| 42 | 7 |
---|
| 43 | 8 |
---|
| 44 | 9 |
---|
| 45 | 10 |
---|
| 46 | }}} |
---|
| 47 | |
---|
| 48 | == Source == |
---|
| 49 | |
---|
| 50 | Partially based on: [http://jcc_7.tripod.com/d/tutor/for_loop.html The for Statement] |