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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The brackets round the 1 to: 10 are required, because we create <strong>an</strong> interval object with the r<strong>an</strong>ge 1 to<br />

10 which is sent the message do: with a block as its parameter. However, it has exactly the same effect<br />

as the C version presented above.<br />

It should be noted that what is happening here is that the block object passed as the parameter to the<br />

do: message actually takes a parameter itself. In fact it is passed the values 1 through 10 in turn. Each<br />

value is the n bound to the block variable n. This is achieved by sending the message value: with a<br />

different element of the interval to the block within the do: message.<br />

In situations where you merely require a piece of code to execute a given number of times, witho ut<br />

the requirement to reference <strong>an</strong>y loop variable, it is possible to use a very simple control structure. This<br />

structure is the timesRepeat: message. This message is sent to <strong>an</strong> integer <strong><strong>an</strong>d</strong> causes the associated<br />

block to be executed n times, where n is the integer the message is sent to. For example:<br />

10 timesRepeat: [Tr<strong>an</strong>script show: 'Hello'; cr.].<br />

this will cause Hello to be printed ten times in the Tr<strong>an</strong>script window.<br />

Blocks with arguments are also used to implement functions to be applied to all element<br />

structure. For example (using <strong>an</strong> array):<br />

s of a data<br />

sum : = 0.<br />

#(3 5 8 4 6) do:<br />

[:item | total := total + ( item * item )]<br />

The variable item takes the value of each element in the array. The result of these expressions is the<br />

sum of the squares of the values in the array. This is a very useful construct.<br />

9.4.3 While loops<br />

The while loop exists in almost all programming l<strong>an</strong>guages. In most cases it has a basic form such as:<br />

while (expression)<br />

statement<br />

Because in <strong>Smalltalk</strong> a while loop is achieved by sending a message to <strong>an</strong> expression the format is<br />

slightly different. In <strong>Smalltalk</strong> the basic format is:<br />

[condition] whileMessage:<br />

[statements]<br />

where condition is the control expression in the form of a Block Context. Statements are the controlled<br />

set of <strong>Smalltalk</strong> code also in a block context. The control of this statement is determined by the<br />

whileMessage that c<strong>an</strong> actually take a number of forms which will be discussed below.<br />

The basic while messages are the whileTrue: <strong><strong>an</strong>d</strong> whileFalse: messages. For the<br />

whileTrue: message, if the value of the control expression in the receiving block is true, then the<br />

controlled statements are executed. After this the control expression is evaluated again <strong><strong>an</strong>d</strong> so on. T his<br />

is actually achieved in <strong>Smalltalk</strong> by sending the receiver block the message value; if the response is<br />

true, then the argument block is sent the message value. This repeats until the receiver block <strong>an</strong>swers<br />

false.<br />

For example:<br />

C Version<br />

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

n = 1; n := 1.<br />

while (n

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

Saved successfully!

Ooh no, something went wrong!