Home Segments Index Top Previous Next

153: Mainline

Blocks may contain answer expressions. If an answer expression is encountered during block evaluation, the value of the answer expression immediately becomes the answer produced by the method in which the block is embedded; nothing else in the method is evaluated. Accordingly, you can move the show: message entirely out of analyzeCalories as defined in Segment 152:

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