Skip to content

For Loops

For loops are designed to execute a block of code n times. That means that you can specify a variable, set an initial value to it and mutate this variable after each loop run, until a certain condition is met. For loops can not only work with integer counters.

Usage

Here is an example of a simple for loop:

Spice
1
2
3
for int i = 1; i <= 10; i++ {
    printf("Round %d\n", i);
}

Optional parentheses

As with the if statement, foreach loop and while loop, the parentheses around the head of the for loop are optional.

Lifetime of the loop variable

The variable declared in the head of the for loop lives as long as the loop. It is constructed once before the first round and destructed once when the loop is left, no matter if this happens through the condition, break or return. It can not be declared again in the body of the loop.

Usage of loop alternatives

For loops should only be used when it is foreseeable how often a block of code will run. If this is not the case, it is recommended to use the while loop or do-while loop instead. If you want to iterate over a container of items, consider using the foreach loop.