Skip to content
justinmann edited this page Dec 24, 2017 · 8 revisions

For loops have very limited functionality which lets them run very quickly.

You can use them to go from a start value to an end value by increment by one:

total = 0
for x : 1 to 10 {
    // This will run 1,2,...,8,9
    total += x
}

The start and end value can be expressions, which will be evaluated before the for loop runs:

func() { 1 }
for x : func() to func() {}

You can run the for loop in reverse

for x : 1 toReverse 10 {
    // This will execute 9,8,...,2,1
}

If start is greater than end then the for loop will not run:

for x : 10 to 1 {
    // This will never run
}

The start and end value must be integers:

// This is invalid
for x : 1.0 to 2.0 {}

For loops do not return value:

// This is invalid
result : for x (1 to 10) {}

Clone this wiki locally