Loops in Smalltalk
to:do:, timesRepeat:, whileTrue: — all messages.
"Loops are messages too: to:do:, timesRepeat:, whileTrue:."
| impressions |
1 to: 3 do: [:run |
Transcript show: 'Proof run ', run printString; cr].
10 to: 4 by: -3 do: [:count |
Transcript show: count printString, ' sheets left'; cr].
3 timesRepeat: [Transcript show: 'pull the bar'; cr].
impressions := 0.
[impressions < 4] whileTrue: [impressions := impressions + 2].
Transcript show: 'Stopped at ', impressions printString; cr.
How it works
1 to: 3 do:counts through a block.by: -3steps downward through the range.[cond] whileTrue: [body]sends whileTrue: to a block.
Keywords and builtins used here
TranscriptcrprintString
The run, in numbers
- Lines
- 13
- Characters to type
- 434
- Tokens
- 79
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 79 seconds.
Step 2 of 3 in Control flow, step 8 of 31 in Language basics.