Home Segments Index Top Previous Next

146: Mainline

Smalltalk permits conditional expressions with ifFalse: messages instead of ifTrue: messages. Whenever an ifFalse: message is sent to true, the block argument is ignored; whenever an ifFalse: message is sent to false, the statements in the block are evaluated. Thus, you can redefine the analyzeCalories method defined in Segment 143 as follows:

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