Anonymous user
Loops/Downward for: Difference between revisions
→{{header|Smalltalk}}
Line 1,798:
=={{header|Smalltalk}}==
<lang smalltalk>10 to:
aNumber displayNl.
]
10 downTo:
aNumber displayNl.
]</lang>
Both enumerate 10 to
Non-Smalltalkers might be confused when seeing:
<lang smalltalk>(10 to:
aNumber displayNl.
]</lang>
which has the same effect, but a slightly different mechanism.
The first one sends a <tt>"to:by:do:"</tt> message to the Integer 10, passing "
The second example first instantiates a range-collection object (called Interval in Smalltalk) with the <tt>"to:by:"</tt> message (sent to the integer), and then this Interval object gets a <tt>"do:"</tt> message.<br>Which - like all collections - enumerates its elements, in this case [10..1].
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:
aNumber displayNl.
]</lang>
to enumerate in a different order,
<br>or combine ranges with a constant array:
<lang smalltalk>(10 to:
aNumber displayNl.
]</lang>
or with a computed array:
<lang smalltalk>(10 to:
aNumber displayNl.
]</lang>
|