Next: , Up: Code blocks (II)


6.7.1 Integer loops

Integer loops are constructed by telling a number to drive the loop. Try this example to count from 1 to 20:

        1 to: 20 do: [:x | x printNl ]

There's also a way to count up by more than one:

        1 to: 20 by: 2 do: [:x | x printNl ]

Finally, counting down is done with a negative step:

        20 to: 1 by: -1 do: [:x | x printNl ]

Note that the x variable is local to the block.

        x

just prints nil.