Loop constructs are all done with methods. These are really just methods of Object. No special syntactic constructs are provided nor needed.
Infinite loop:
loop("foo" println)
While loop:
# while(condition, message)
a := 1
while(a <= 10,
a print
a = a + 1
)
For loop:
for(i, 1, 10,
i println
)
Reverse for loop:
for(i, 10, 1,
i println
)
The variable used inside the loop does not have to be defined beforehand. It is, however, available afterwards (and will have the value it had when the loop ended... in this case, 10).
Note: for(i, 1, 10, ...) will loop 10 times. for(i, 0, 10, ...) will loop 11 times.
Also: the "start" and "end" values are evaluated only once, before the loop starts. Putting something mutable in there, in the hope to change the loop while it's running, won't have the desired effect.
Repeat:
3 repeat("foo" print)
This is a really a method of List, and roughly corresponds to Python's for ... in ... statement. There are several variants.
foreach with one named argument:
a := list(1, 2, 3) a foreach(x, x print) #=> 123
foreach can also be called with two named arguments. The first one is an index (starting at 0), the second one is an element of the list.
names := list("hans", "mari", "kavi")
Io> names foreach(i, x, writeln(i, " => ", x))
# 0 => hans
# 1 => mari
# 2 => kavi
Last but not least, foreach can be called with just a body and no named arguments. The body is interpreted as a message that will be sent to each object in the list. In order for this to work, the object must of course support that message.
names foreach(println) names foreach(asUppercase println)
The control flow operations break and continue should work in any of these loops.
for(i, 1, 10,
if(i > 5, break)
i println
)