Home Segments Index Top Previous Next

152: Mainline

The answer produced by an ifTrue:, ifFalse:, ifTrue:ifFalse:, or ifFalse:ifTrue: message is the value produced by the evaluated block. The value produced by an evaluated block is the value of the expression that constitutes the final statement in the block. Accordingly, you can move the show: message out of the block in analyzeCalories as defined in Segment 151:

Integer method definition • instance 
analyzeCalories 
  Transcript show: 
    (self > 1600 
       ifTrue: ['You have had enough'] 
       ifFalse: [(self < 1200) 
                   ifTrue: ['You need more'] 
                   ifFalse: ['You have eaten wisely']]); 
             cr 
Workspace
1700 analyzeCalories. 
1400 analyzeCalories. 
1100 analyzeCalories 
Transcript 
You have had enough 
You have eaten wisely 
You need more 

Note that the entire conditional expression is surrounded by parentheses; otherwise, Smalltalk would look for a show:ifTrue:ifFalse method, and, failing to find one, Smalltalk would complain.