Home Segments Index Top Previous Next

143: Mainline

Suppose, for example, that you want to define a method that displays a message that depends on the number of calories consumed in a day. Specifically, if the number of calories is greater than 1600, you want your method to display You have had enough; if the number is less that 1200, you want your method to display You need more.

One solution is to write a method that uses ifTrue: statements in which the embedded statements display a string in the transcript:

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