Contents / Part 3 / Previous chapter / Next chapter / Index


26 Operator precedence

An operator is something like +, /, < etc, which affects one or more items - for example comparing, them, or adding them.

Mathematical operators are familiar. Most act on two numbers- for example

3+7 addition

2-5 subtraction

4*6 multiplication

1/9 division

7 DIV 4 integer division

7 MOD 4 integer remainder

3 ^ 4 raise to a power

These operators are referred to as binary operators since they require two operands (i.e., two things to operate on)

-5

This shows one of the key 'unary' operators that we are used to - the - just acts on the 5 to make it a negative number.

This version of BASIC: has a large number of operators and it is very important that the user is aware of their order of precedence. You will remember that in mathematics multiplication must be completed before addition - the same applies to other operators - there is a strict hierarchy and you must be aware of it if the computer is to do what you expect.

The overall order of precedence (or operators is as follows.

Group 1

Group 2

Group 3

Group 4

Group 5

Group 6

Group 7

All operators in each group have equal priority and will be dealt with on a left to right basis - in other words in order in each line.

Some of the operators should be familiar by now; others may need explanation.

Group 1

NOT is most often used to reverse the result of a test. e.g. IF NOT(X=5)THEN... Clearly this example could be written IF X <> 5 THEN but the operator NOT is often essential when using functions e.g. IF NOT FNVALID THEN...

Functions include all the predefined functions such as SQR, SIN, ASC etc and user defined functions like FNVALID.

Brackets can be used to ensure that everything within the brackets is evaluated before any other calculations take place. Indirection operators are described in section 39 on page 411.

Group 2

Raise to the power e.g.

3^2=9

3^3=27

Group 3 and Group 4

These contain all the usual arithmetic operators. Nothing unexpected here.

Group 5

This contains the relational operators which mean 'greater than', 'less than', etc. They are used in expressions such as

IF X>10 THEN . . .

Group 6

Logical AND is used to ensure that two or more conditions hold true before some action is taken e.g.

IF X>10 AND Y=6 THEN . . .

For further details see the entry on page 205

Group 7

Logical OR is also used with multiple conditions e.g.

IF X>10 OR Y=6 THEN

The action is taken if one or more of the conditions is true. EOR is normally only used as a bitwise operator and the user is referred to page 250 for details.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001