Tutorial. Precedence, Unary Binary and Keyword Messages, printString,
errors, using parenthesis .
Tutorial 2. Precedence, printString, & errors
| 1. Starting Smalltalk, and Terminology | 2. Precedence Rules | 3. Classes - Strings | 4. Class Browser | 5. Variables | 6. Error Messages | 12. Control Structures | 16. Dialogs | 23. Collection class | 39. OpenGUI |
| Smalltalk Books | Student Software & Books | Hints & Tips | Sites | Download Smalltalk | Coloured code | Download Documents | ASCII Codes |
Question 1. What do you think the result of the following expression will be.
2 + 3 * 4 - 1
Go to Answer 1
2 + 3 * 4 - 1Enter the above, in the Workspace and evaluate it, and check the answer is as stated in Answer 1.
Messages are sent to receivers (a receiving object) to make that object do something.
afrog colour: Red
2 + 3
The receivers in the above are aFrog, 2, and myAccount.
When a Smalltalk expression contains different types of messages, then the rules of precedence that decide the order of evaluation are:
+ * - /i.e. plus, minus, multiply and divide are all binary messages, and all messages within a type have the same level of precedence.
Therefore the rule that is used in the evaluation of
2 + 3 * 4 - 1is that the evaluation is performed left to right.
|unary||Used as follows. receiver unaryMessage
A message that does not have any arguments. E.g. size, asInteger, negated, reversed, asUppercase
Easy to recognise, there are no arguments.
|binary||Used as follows. Receiver
Binary messages have a parameter.
Binary messages are used for:-
Arithmetic, e.g. +, -, *, /, //.
Comparisons and logical operations. E.g.=, <, >, <=, >= Think of these messages as asking a question, e.g. is number1 equal to number2.
Easy to recognise, there is an argument and the message selector is not a word, or alpha numeric character.
3 + 2
( number1=number2 )
|keyword|| Used as follows. Receiver
A keyword message must have 1 or more arguments and is recognisable by the : colon.
Message with 1 argument age:
Do NOT place a space between the keyword and its :
e.g. age : is wrong
Message with 2 arguments.
Used as follows. Receiver binaryMessageColon argument binaryMessageColon argument
Note how they are written without spaces when talking about the message without the parameters
Easy to recognise, there is at least 1 argument. The message selector ends in a colon.
|aperson age: 18
aFrog position: 4
aCollection detect: [ aBlock ]
Blocks are discussed later
aCollection at: anIndex put: anObject
'abcde' copyFrom: 2 to: 4
Very Important. Make sure you can pick out the
receiver, message selector, and argument in the examples above. They are colour coded to make it easier.
You will very quickly start to struggle in the M206 OU course if you do not pick up the technique of recognising these easily. In the examples given it is not to difficult to pick these out. Later when you get several messages combined on one line it becomes more difficult.
As stated above, where messages are of the same type as in
2 + 3 * 4 - 1 , the order used is
left to right.
2 + ( (3 * 4) - 1 )
evaluated last ( (evaluated first) evaluated second )
Parenthesis can be used when the brackets are not actually required, to emphasis the actual order. This technique is used a lot in the M206 Open University course.
Check your understanding of these rules by calculating the answers of the following expressions in your head, according to the above rules. Check your answer by evaluating them in the Workspace
plus:message is only available in the M206 version of LearningWorks.
Go to Answer 2
Check the above answer by evaluating them.
Examination of the method plus: How this is done is introduced later in these tutorials.
|Method heading consisting of the name + and argument aNumber|
|The method body No helpful comment.|
^self + aNumber
|The code for the method. Again
as in the + method you have the caret ^ character,
self and the argument aNumber.
But have you noticed that the message + is being sent to self in the code.
The two methods are doing the same.
You already know that
2 + 3 * 4 - 1
2 plus: 3 * 4 - 1
Check the above answers by evaluating
2 plus: 3 * 4 - 1
Read Tip 1. Make a list of Error Messages Then return to this point
Evaluate 2 +
Because + is a binary message selector, and thus should be followed by an argument, an error message appears as follows:-
2 + Argument expected->
Before pressing the enter key to delete the highlighted error message, note the following:
When you have deleted the a highlighted error message, enter the argument e.g. 3 so that the expression reads
When you checked out the list of error messages did you notice that this exception message can be 2 + 3
Evaluate to ensure this works.
Strong recommendation: If you have not started to make out a list of error messages. Start one now.
is "Answer a String whose characters are a description of
In Smalltalk a string starts and ends with a single quote, E.g. 'Hello World' , '1234' etc.
Example Usage 4 printString
would answer with '4' which you can use if required.
When printString is sent to an object a string is returned.
Question 5 Is printString a unary, binary or keyword message.
Go to Answer 5
Question 6 What do you think (2 + 3) printString will evaluate to?
Go to Answer 6
(2 + 3) printStringin the Workspace
(a). List the 3 types of message in the order of precedence in which they are carried out.
(b). What can you use to force a change in the order of precedence?
(c). What is the general rule that is used for the order of precedence?
(d). Which message type is very easy to recognise because it has a colon: in it?
(e). Which 2 types of message always have an argument?
(f). Which message can have 1 or more arguments?
(g). Fill in the missing word. A unary message is easy to recognise because it does NOT have an ......
(h). What message type is printString?
(i). If the evaluation of 5 printString Answers with '5' . What do you think the result of 2 + 3 printString will be, and why?
When you have read the answer to (i).
If you are a mathematician or have programmed in other languages you may think that the answer is 13, because usually
* and / have a higher precedence than + and - .
This is NOT the case with Smalltalk.
Evaluation will show that the expression 2 + 3 * 4 - 1 answers 19 when evaluated.
Smalltalk's precedence rules are different.
Return to Question 1
Return to Question 2
2 plus: 3 * 4 - 1 answers with 13
(Note this is a corrected answer from earlier versions, where the answer order had got out of sink with the questions)
2 plus: 3 * 4 - 1 contains a mix of keyword:- plus:, and binary:- *,-, message selectors.
Binary takes precedence over keyword selectors.
The 2 binary selectors evaluate left to right. If parenthesis are added to emphasis the order we get.
2 plus: ( (3 * 4) - 1 )
Doing the calculation in order we get these stages.
2 plus: ( (12) - 1 )
2 plus: ( 11 )
Return to Question 4
printString is a unary message.
Return to Question 5
Note the single quotes.
printString is a unary message and has a higher precedence than the + binary message. Therefore the first expression evaluated is 3 printString . This produces an answer of '3' a string.
Lets try this now.
2 + 3 printStringand study the Unhandled exception: box that pop up.
Click on the cancel button. Change the example to
(2 + 3) printStringNow work out why this works correctly and answers '5'.
Its all to do with the precedence rules.
Return to Test questions
Leeds & the UK Tourist Information & guide my home town
Relax in the sun.
Copyright © John McGuinn 2000 - 04