Home Segments Index Top Previous Next

164: Mainline

In the following example, the argument block in the and: expression is evaluated only if self is greater than 1200. The argument block in the or: expression is evaluated only if self is not greater than 1200.

Note that the parentheses are mandatory; without them, Smalltalk would think you were trying to use and:ifTrue: and or:ifTrue: messages:

Integer method definition • instance 
analyzeCalories 
  (1200 <= self and: [self <= 1600]) 
    ifTrue: 
      [Transcript show: self printString; show: ' is reasonable'; cr]. 
  (self < 1200 or: [self > 1600]) 
    ifTrue: 
      [Transcript show: self printString; show: ' is unreasonable'; cr] 
Workspace
1700 analyzeCalories. 
1400 analyzeCalories. 
1100 analyzeCalories 
Transcript 
1700 is unreasonable 
1400 is reasonable 
1100 is unreasonable