29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ifFalse: [.....]<br />

However, it is easy to get confused <strong><strong>an</strong>d</strong> therefore one must be careful. A facility not provided<br />

explicitly by <strong>Smalltalk</strong> is the if -then-elseif-else type of structure. For example, in C it is possible to<br />

write:<br />

if (n < 10)<br />

printf (“less th<strong>an</strong> 10”);<br />

else if (n < 100)<br />

printf (“greater th<strong>an</strong> 10 but less th<strong>an</strong> 100”);<br />

else<br />

printf (“greater th<strong>an</strong> 100”);<br />

In <strong>Smalltalk</strong> it is necessary to nest ifTrue:ifFalse constructs as above. However, it is easier to<br />

see if you have a d<strong>an</strong>gling else problem as the built -in formatter available in the various browsers c<strong>an</strong><br />

be used to see if the code formats in the expected m<strong>an</strong>ner.<br />

There is also no such thing as a case statement in <strong>Smalltalk</strong>. Instead, the fun ctionality required is<br />

usually achieved using a dictionary.<br />

map := Dictionary new.<br />

map at: $^ put: [Tr<strong>an</strong>script show: 'It is a caret'].<br />

map at: $> put: [Tr<strong>an</strong>script show: 'It is greater th<strong>an</strong>'].<br />

map at: $< put: [Tr<strong>an</strong>script show: 'It is less th<strong>an</strong>']<br />

:<br />

result := (map at: char) value.<br />

This is the <strong>Smalltalk</strong> equivalent of the following in C:<br />

switch (char) {<br />

case '^':<br />

printf(“It is a caret”);<br />

break;<br />

case: '>':<br />

printf(“It is a greater th<strong>an</strong>”);<br />

break;<br />

:<br />

This is actually <strong>an</strong> example of how Smallta lk is truly object oriented. That is, even what would be<br />

considered st<strong><strong>an</strong>d</strong>ard control structures in other l<strong>an</strong>guages, have to be implemented by using objects <strong><strong>an</strong>d</strong><br />

message passing. A point to note is that the control structures which do exist in <strong>Smalltalk</strong> may at first<br />

seem to be similar to the control structures in other l<strong>an</strong>guages. However, this is both persuasive <strong><strong>an</strong>d</strong><br />

misleading. They are of course messages to objects (e.g. true, false or a block object). Internally they<br />

perform something which is similar in na ture to what we have done above for the switch statement. If<br />

you find this confusing, don’t worry, but if you c<strong>an</strong> see what is me<strong>an</strong>t by this then you have gone a long<br />

way down the round to underst<strong><strong>an</strong>d</strong>ing <strong>Smalltalk</strong>.<br />

9.4.2 Repetition<br />

Iteration in <strong>Smalltalk</strong> is accomp lished with the timesRepeat:, whileTrue:, whileFalse: <strong><strong>an</strong>d</strong><br />

enumerating messages (such as do:). The enumerating messages will be considered in more detail when<br />

we discuss the collection classes. For the moment we shall limit ourselves to a simple form of the do:<br />

message <strong><strong>an</strong>d</strong> the timesRepeat message. In the next subsection we will consider the while -based<br />

messages.<br />

Like their counter -parts in other l<strong>an</strong>guages, the timesRepeat: <strong><strong>an</strong>d</strong> do: me ssages repeat a<br />

sequences of instructions a fixed number of times. In C, this construct is provided by the for loop. For<br />

example:<br />

for (n = 1; n

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!