Jump to content

Smalltalk Programming/Control

From Wikibooks, open books for an open world
(Redirected from Programming:Smalltalk/Control)

Program flow within a method is managed with [blocks]. A block is an object containing some smalltalk code, and it can be sent methods to accomplish any program flow needed. Outside of a single method body, program flow is decided in sending the same method to dissimilar objects; the classic example is to send a circle to either to a monitor, printer, or X/Y plotter.

Please feel free to replace the following examples with something less..pointless.

1 to: 5 do: [num| transcript print:num]. - putting a flow control method like to:do: inside a Number class may seem a little funny, but it makes this easy to express.

num > 5 ifTrue: [num:=1] ifFalse: [num:=num+1]. - feel free to split the ifTrues and ifFalses across different lines.

[num < 5 ] whileTrue: [num:=num+1. transcript print:'embiggen!']. Yes, a block can be the opening object in a statement.