March 19, 2024

Smalltalk Usage and Syntax

 

The following examples illustrate typical usage and syntax of the language.

An example of a Smalltalk method that uses the default message answer.

initialize

   "Initialise as for the superclass and set height to 0.
   Answer the receiver."
   super initialize.
   self height: 0

A method with an explicit answer expression.

height

   "Answer the height of the receiver."
   ^height

Next is an example of condition and selection:

(bg3Account balance > 1000000 and: [(bg3Account overLimit) >= 1000000])
   ifTrue: [Dialog warn: 'Status: Platinum']
   ifFalse: [((bg3Account balance) > 100000)
      ifTrue: [Dialog warn: 'Status: Gold']
      ifFalse: [Dialog warn: 'Status: Silver']].

The following code illustrates the use of assignment, collection classes, Date objects and blocks with either one or two arguments:

birthDays := Dictionary new.
birthDays
   at: 'Monet' put: (Date newDay: 14 month: #November year: 1840);
   at: 'Pollock' put: (Date newDay: 28 month: #January year: 1912);
   at: 'de Kooning' put: (Date newDay: 24 month: #April year:1904);
   at: 'Turner' put: (Date newDay: 23 month: #April year: 1775);
   at: 'Cezanne' put: (Date newDay: 19 month: #January year: 1839);
   at: 'Guston' put: (Date newDay: 27 month: #June year: 1913).

twentiethCenturyArtists := SortedCollection new.
birthDays keysAndValuesDo: [ :who :when |
   (when year between: 1900 and: 1999)
      ifTrue: [ twentiethCenturyArtists add: who ]].
artistsString := 'List of 20th century artists in alphabetic order:'.
twentiethCenturyArtists do: [ :name |
   artistsString := artistsString, '\', name ].
Dialog warn: artistsString withCRs.

This code creates an array of integers and initialises the total to zero before looping through the array.

scores := #( 78 67 82 81 77 77 76 ).
total := 0.
scores do: [ :each | total := total + each].
average := ( total / 7 ) rounded.
Dialog warn: 'Your average score was ' , average printString.

The following code uses to:by:do: that requires three arguments. The temporary variable is declared within the block.

name := 'Victoria & Albert Museum'.
numberOfConsonants := 0.
1 to: name size do: [ :index | | letter |
   letter := name at: index.
   (letter isAlphabetic and: [ letter isVowel not ])
      ifTrue: [ numberOfConsonants := numberOfConsonants + 1] ].

Finally an example of a whileTrue: loop. The temporary variables are declared outside of the block. Comments are useful for adding clarity to the code.

| rats generations mothers babies |
"This makes a number of assumptions, including a female having a litter of 10 every 21 days"
rats:= 2.
generations := 1.
[ rats < 3000 ] whileTrue:
   [ mothers := (rats/2) asInteger. "avoids fractional rats"
   babies := mothers * 10.
   rats := rats + babies.
   generations := generations + 1.
   Dialog warn: 'After ', (generations * 21) printString,
   ' days the colony has ', rats printString, ' rats'
   ]

« Back to FAQ page

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Up to top of page