Sinclair QL Beginner's Manual page 61

Hide thumbs Also See for QL:
Table of Contents

Advertisement

NUMERIC OPERATIONS
SuperBASIC allows the usual mathematical operations. You may notice that they are like functions
with exactly two operands each. It is also conventional in these cases to put an operand on each side
of the symbol. Sometimes the operation is denoted by a familiar symbol such as + or *. Sometimes
the operation is denoted by a keyword like DIV or MOD but there is no real difference. Numeric
operations have an order of priority. For example, the result of:
PRINT 7 + 3*2
is 13 because the multiplication has a higher priority. However:
PRINT (7+3)*2
will output 20, because brackets over-ride the usual priority. As you will see later so many things can
be done with SuperBASIC expressions that a full statement about priority cannot be made at this
stage (see the Concept Reference Guide if you wish) but the operations we now deal with have the
following order of priority:
highest - raising to a power
multiplication and division (including DIV, MOD)
lowest - add and subtract
The symbols + and - are also used with only one operand which simply denotes positive or negative.
Symbols used in this way have the highest priority of all and can only be over-ridden by the use of
brackets.
Finally if two symbols have equal priority the leftmost operation is performed first so that:
PRINT 7-2 + 5
will cause the subtraction before the addition. This might be important if you should ever deal with
very large or very small numbers.
Operation
Add
Subtract
Multiply
Divide
Raise to power
Integer divide
Modulus
Modulus returns the remainder part of a division. Any attempt to divide by zero will generate an error
and terminate program execution.
NUMERIC EXPRESSIONS
Strictly speaking, a numeric expression is an expression which evaluates to a number and there are
more possibilities than we need to discuss here. SuperBASIC allows you to do complex things if you
want to but it also allows you to do simple things in simple ways. In this section we concentrate on
those usual straightforward uses of mathematical features.
Symbol
Examples
+
7+6.6
-
7-6.6
*
3*2.1
2.1*(-3)
/
7/2
-17/5
^
4^1.5
DIV
-8 DIV 2
7 DIV 2
MOD
13 MOD 5
21 MOD 7
-17 MOD 8
Results
Note
13.6
0.4
6.3
-6.3
3.5
Do not divide by zero
-3.4
8
-4
Integers only
3
Do not divide by zero
3
0
7

Advertisement

Table of Contents
loading

Table of Contents