Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
assigned <strong>an</strong> initial value before the condition expression. This is because the while message begins by<br />
evaluating the control expression, if no initial value is provided for n it will default to nil. Comparing nil<br />
with a numeric value will result in <strong>an</strong> exception being raised.<br />
As you c<strong>an</strong> see the equivalent C code is not that diffe rent, but the sem<strong>an</strong>tic me<strong>an</strong>ing is completely<br />
different.<br />
The whileFalse: message has exactly the same format as the whileTrue: message. The only<br />
difference is in the condition used to decide whether to evaluate the second block or not.<br />
n := 1.<br />
[n > 10] whileFalse:<br />
[Tr<strong>an</strong>script show: n.<br />
n := n + 1].<br />
In some cases we w<strong>an</strong>t to do all the work in the receiver block. These versions of the while message<br />
c<strong>an</strong> be viewed as being similar to the do statement in C. To support such a feature there are two<br />
versions of the while loop which do not require the second block to be provided. These are the<br />
whileTrue <strong><strong>an</strong>d</strong> whileFalse messages (note the absence of the ‘:’). For example:<br />
C Version<br />
do {<br />
sc<strong>an</strong>f(“%d”, &n);<br />
printf(“%d\n”, n);<br />
} while (N < 1000);<br />
<strong>Smalltalk</strong> Version<br />
[n := Tr<strong>an</strong>script getNumber.<br />
Tr<strong>an</strong>script show: n; cr.<br />
n < 1000] whileTrue.<br />
Both of these segments of code are expected to read in a number, print it back out <strong><strong>an</strong>d</strong> then check to see<br />
if it is less th<strong>an</strong> 1000. If it is th e sequence of steps is repeated, otherwise the loop is terminated. For<br />
example, this could be used to ensure that some input is in the desired r<strong>an</strong>ge.<br />
9.5 Summary<br />
You now know virtually everything about <strong>Smalltalk</strong> except the classes which make up the very large<br />
library of reusable components available to the developer. You have learnt the basics of the l<strong>an</strong>guage in<br />
chapter 6 <strong><strong>an</strong>d</strong> in this chapter you have learnt about cascading, blocks <strong><strong>an</strong>d</strong> their use in iteration<br />
constructs. You have also learnt about conditional sta tements <strong><strong>an</strong>d</strong> their use of blocks. You are now<br />
ready to explore the class structure in <strong>Smalltalk</strong>.<br />
9.6 Further Reading<br />
As ever, the Hopkins <strong><strong>an</strong>d</strong> Hor<strong>an</strong> book [Hopkins <strong><strong>an</strong>d</strong> Hor<strong>an</strong> 1995] is a good reference source for the<br />
<strong>Smalltalk</strong> l<strong>an</strong>guage. Other useful books are [Lalonde <strong><strong>an</strong>d</strong> Pugh 1991] <strong><strong>an</strong>d</strong> [Goldberg <strong><strong>an</strong>d</strong> Robson 1989].<br />
10. The Collection Classes<br />
10.1 <strong>Introduction</strong><br />
This chapter discusses probably the most used class hierarchy in <strong>Smalltalk</strong>; the collection class<br />
hierarchy. The collection classes are the basis upon which data struc tures are constructed in <strong>Smalltalk</strong>.<br />
Section two introduces the collection class hierarchy <strong><strong>an</strong>d</strong> the common functionality provided by all<br />
collection classes. Section three presents a decision tree to help identify the most appropriate collection<br />
class to use. Sections four, five <strong><strong>an</strong>d</strong> six present the Bag class, the Set class <strong><strong>an</strong>d</strong> the<br />
86