Loops/Downward for: Difference between revisions

m
Line 1,800:
<lang smalltalk>10 to: 0 by: -1 do:[:aNumber |
aNumber displayNl.
].
10 downTo: 0 do:[:aNumbereachNumber |
aNumbereachNumber displayNl.
]</lang>
Both enumerate 10 to 0 inclusive.
 
Non-Smalltalkers might be confused when seeing:
<lang smalltalk>(10 to: 0 by: -1) do:[:aNumbereachNumber |
aNumbereachNumber displayNl.
]</lang>
which has the same effect, but a slightly different mechanism.
Line 1,819:
 
The nice thing with Intervals is that they can be concatenated with a <tt>","</tt> operator (like all collections); thus, I could also write:
<lang smalltalk>(10 to: 5 by: -1),(0 to: 4) do:[:aNumbereachNumber |
aNumbereachNumber displayNl.
]</lang>
to enumerate in a different order,
<br>or combine ranges with a constant array:
<lang smalltalk>(10 to: 0 by: -2),#(99 999),(1 to: 9 by: 2) do:[:aNumbereach |
aNumbereach displayNl.
]</lang>
or with a computed array:
<lang smalltalk>(10 to: 0 by: -2),{ 10 factorial . 11 factorial},(1 to: 9 by: 2) do:[:aNumbereach |
aNumbereach displayNl.
]</lang>
 
Anonymous user