For
The for
construct it's usually used for iteration over collections and for counting.
The following loop counts from 0 to 9:
Alternatively, using a simpler form of the for-loop:
To iterate over an array, Sidef supports yet another form of the for
loop:
The for-in
loop is a generalization of the for
loop, supporting iteration over any object that respond to the iter
method, which must return a block as the iterator, and the block should return one value at a time when it's called, or nil
when there are no other values to return, which will break the loop.
Alternatively, the object may also respond to the to_a
method, which must return an object of type Array
. Using this knowledge, we can iterate over Hash
objects because the Hash
class implements the to_a
method:
The for-in
loop can also be used in iterating over matrices:
Last updated