Navigation:  Tutorials > Beginners Guide to Smalltalk > Control Flow >

The Beauty of #ifTrue: (and friends)

Previous pageReturn to chapter overviewNext page

If you are used using other computer languages using these messages may look a little odd at first. You're probably used to using something like:

if (simon radius > charlie radius) {

       printf("Simon is bigger than Charles") }

 

in which case, the Smalltalk equivalent:

(simon radius > charlie radius) ifTrue: [

       MessageBox notify: 'Simon is bigger than Charles'].

 

looks somewhat "backwards" and perhaps harder to read. I know that I originally thought this when learning Smalltalk for the first time. However, I guarantee that you'll soon get used to it.

The beauty comes when you realize that #ifTrue: is a message and not part of the Smalltalk language definition at all. The grammar of the traditional language includes the definition of how an "if" statement is formed but Smalltalk needs no such definition. Instead, the control flow is implemented solely with messages. This makes the "language" smaller, more regular, and easier to learn because it has many fewer elements to it. It is also more flexible since you could (if you wished) define new messages to perform similar operations.

Of course, all this also applies equally to the other control flow messages: #whileTrue:, #whileTrue, #whileFalse:, #whileFalse, #repeat, #timesRepeat:, #to:do: and #to:by:do:.