Loop structures: Difference between revisions

Content added Content deleted
(Old example was silly and misleading: iterated over a size-1 array containing the range 0..4, instead of iterating over the range 0..4)
Line 841: Line 841:
==[[Smalltalk]]==
==[[Smalltalk]]==
[[Category:Smalltalk]]
[[Category:Smalltalk]]

===timesRepeat===

10 timesRepeat: [ expression ].

===iterating over a collection===

( collection ) do: [ expression ].

( collection ) collect: [:element | element sendMessageToElement].

#(1 2 3 4) inject: 5 into: [:sum :number | sum + number]

===whileTrue/whileFalse===
===whileTrue/whileFalse===


Line 849: Line 862:
[ x = 0 ]
[ x = 0 ]
whileFalse: [ x := x - 20.].
whileFalse: [ x := x - 20.].

===simple looping===

1 to: 5 do: [:number | Transcript show: 'Here is a number: ' number printString ; cr]

(-50 to: 50 by: 4) do: [:number | Transcript show: number ; cr]


==[[Tcl]]==
==[[Tcl]]==