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.

9.3.3 Block temporary variables<br />

Blocks c<strong>an</strong> also possess their own temporary variables. These variables are defined between two<br />

vertical bars <strong><strong>an</strong>d</strong> after <strong>an</strong>y input parameters. For example:<br />

[:x |<br />

|temp1 temp 2|<br />

temp1 := x.<br />

temp2 := temp1 * 3.]<br />

A block c<strong>an</strong> actually have up to 256 temporary variables in VisualWorks. However this figure does<br />

vary from implementation to implementation.<br />

9.3.4 Typical block usage<br />

Finally, blocks are often used for control structures:<br />

aNumber even<br />

ifTrue: [aString := 'even']<br />

ifFalse: [aString := 'odd'].<br />

Effectively this me<strong>an</strong>s send the message valu e to one of the blocks, depending on the result of<br />

testing aNumber to see if it is even or odd. We shall look at the use of blocks in condition <strong><strong>an</strong>d</strong> iteration<br />

statements later in this chapter.<br />

9.4 Control structures<br />

9.4.1 Flow of control<br />

As has previously been menti oned, the if -then constructs in <strong>Smalltalk</strong> are actually methods defined on<br />

the class Boole<strong>an</strong>. However, ignoring that issue for a moment, the actual use of the structures is very<br />

straight forward. The basic formats of the if-then expression are:<br />

aBoole<strong>an</strong><br />

ifTrue: aBlock<br />

ifFalse: <strong>an</strong>otherBlock.<br />

aBoole<strong>an</strong><br />

ifFalse: aBlock<br />

ifTrue: <strong>an</strong>otherBlock.<br />

aBoole<strong>an</strong><br />

ifTrue: aBlock.<br />

aBoole<strong>an</strong><br />

ifFalse: aBlock.<br />

The boole<strong>an</strong> object is often generated dynamically via some form of logical test (e.g . a < b ).<br />

That is, the first operation is to create the boole<strong>an</strong> object, which is then sent the message<br />

ifTrue:ifFalse:. This is why the boole<strong>an</strong> test is often bracketed with round brackets. Then if the<br />

value of the boole<strong>an</strong> is true, the code in the ifTrue block is executed, if it is false the code in the ifFalse<br />

block is executed. Consider the following example:<br />

C version<br />

<strong>Smalltalk</strong> Version<br />

if (count < 100) (count < 100)<br />

count++; ifTrue: [count := count + 1]<br />

else {<br />

ifFalse: [Tr<strong>an</strong>script show:<br />

printf(“Overflow\n”);<br />

'Overflow'.<br />

count = 0;<br />

Tr<strong>an</strong>script cr.<br />

} count := 0.]<br />

In both cases the code increments the value of a counter if its ma ximum count has not been reached;<br />

if the maximum count has been reached, the code resets the counter <strong><strong>an</strong>d</strong> prints <strong>an</strong> error message. Nested<br />

if-then statements c<strong>an</strong> be constructed as in <strong>an</strong>y other l<strong>an</strong>guage. For example, in <strong>Smalltalk</strong>:<br />

(count < 100)<br />

ifTrue: [(index < 10)<br />

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

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

83

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

Saved successfully!

Ooh no, something went wrong!