Navigation:  Appendix A: Jumping Into Smalltalk >

Loops

Previous pageReturn to chapter overviewNext page

Smalltalk uses blocks to do looping. Since blocks are just objects, we can send messages to them.

Java

Smalltalk

int tries = 0;
while (tries <= 5) {
tryAgain();
tries++;
}

| tries |
tries := 0.
[tries <= 5] whileTrue: [
self tryAgain.
tries := tries + 1]

alternatively:

5 timesRepeat: [self tryAgain]

Notice that timesRepeat: is a message understood by Integers. It simply evaluates the block the proper number of times.