Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
BASIC Arithmetic Operators
◄Expressions and Operators► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
Arithmetic Operators
The BASIC arithmetic operators are listed in order of precedence:
Operator Meaning
^ Exponentiation
- Negation
*,/ Multiplication and division
\ Integer division
MOD Modulo division
+,- Addition and subtraction
Parentheses change the order in which arithmetic operations are performed.
Operations within parentheses are performed first. Inside parentheses, the
usual order of operation is maintained.
Generally, two consecutive operators must be separated by parentheses.
Exceptions to this rule are * -, * +, ^ -, and ^ +.
Integer Division
Integer division is denoted by the backslash (\) instead of the forward
slash (/), which indicates floating-point division. Before integer division
is performed, operands are rounded to integers or long integers, and so
must be greater than -2,147,483,648.5 and less than 2,147,483,647.5. The
quotient of an integer division is truncated to an integer.
Example
PRINT 7 \ 2, 7 / 2, -32768.499 \ 10, -32768.499 / 10
Output
3 3.5 -3276 -3276.8499
Modulo Arithmetic
Modulo arithmetic is denoted by the modulus operator MOD. Modulo arithmetic
provides the remainder, rather than the quotient, of an integer division.
Example
X% = 7 \ 2
REMAINDER% = 7 - 2*X% '7 = 2*3, with remainder 1
PRINT REMAINDER%, 7 MOD 2
Output
1 1
Overflow and Division by Zero
Dividing by zero, raising zero to a negative power, or arithmetic overflow
produce run-time errors. These errors can be trapped by an error-trapping
routine. The following BASIC statements are useful for error trapping:
◄ON [LOCAL] ERROR► - error handling
◄RESUME► - continue execution after error-trapping
◄ERR, ERL► - error status
◄ERROR► - allows user to define error codes