# for loop proc

Source: https://ref.dm-lang.org/proc/for/loop

### Format
> `for(Init, Test, Inc) Statement`

First execute Init. Then if Test is true (non-zero), execute Statement. After this execute Inc. Continue checking Test, doing Statement, and performing Inc until Test turns out to be false (zero).

Statement may be a code block or a single statement. Semicolons may be substituted for commas inside the parentheses as a convenience to C/C++ programmers.

Init and Inc may be omitted. If Test is omitted, the loop will continue forever (unless a break, goto, or return instruction is used to get out of the loop).

```dm /for loop/
var/i
for(i=0, i<3, i++)
    world << i
```

This outputs:

```dm /for loop/
0
1
2
```

> [!note]
> 
> An Inc statement like `i += 0.1` is perfectly valid, but you should keep in mind that numerical accuracy is not exact. See [Numbers](https://ref.dm-lang.org/notes/numbers.md) for more information.

## C-like syntax

Using the [`#pragma syntax` directive](https://ref.dm-lang.org/DM/preprocessor/pragma/syntax.md) you can change `for()` to be more like other languages such as C. This uses a semicolon `;` in place of commas to separate Init, Test, and Inc. Instead the comma is treated as a way of stringing multiple statements together.

```dm /for loop/
// make this syntax change temporary
#pragma push
#pragma syntax C for

var/i,j
for(i=j=0; i<=10; ++i,j+=i)
    world << "A triangle [i] block\s high has [j] block\s total."

#pragma pop
```

### See also
- [break statement](https://ref.dm-lang.org/proc/break.md)
- [continue statement](https://ref.dm-lang.org/proc/continue.md)
- [do proc](https://ref.dm-lang.org/proc/do.md)
- [for list proc](https://ref.dm-lang.org/proc/for/list.md)
- [while proc](https://ref.dm-lang.org/proc/while.md)
