This project is read-only.

For loop

The iterator loop (a.k.a. for loop) allows traversing over a set of values, executing a block of code for each one. It requires an array of values, a range or a dict to get values from.

The iterator loop defines a new local variable that contains the current item from the array or range. For a range, it's type is always integer, for a dict - string, and for the array it depends on the array type.

for i in 1..5 do
  println i, " "

The code above will output:
1 2 3 4 5

Iterating an array is just as easy:

var arr = new [ 5 4 3 2 1 0 ]
for curr in arr do
  print curr, " " 

The code above will output:
5 4 3 2 1 0

Key and value iteration

Since Mirelle 1.4 the for loop can provide key as well as value. It's especially useful when iterating over a dict - moreover, you can't just iterate a bare dict without specifying both key and value. The syntax is as follows:
for key => value in expr do
   print key, " ... ", value 

The key and value syntax is applicable to range and array iteration as well. The key variable will store the iteration index.
Related articles: Break and Redo

Last edited May 6, 2011 at 8:19 PM by impworks, version 2


No comments yet.