Skip to content

Operators

Operators in ZX Basic can be arithmetical, logical and bitwise ones. For bitwise operators checks the Bitwise Operators page.

Arithmetic Operators

Arithmetic operators are left associative (like in Sinclair BASIC) and have the same precedence. The following are a list of arithmetic operators (lower precedence operators appears first):

  • +, - (Addition, Subtraction)
    Operator + (addition) can also be used with strings to perform string concatenation.

  • *, /, mod (Multiplication, Division and Modulo)

Note: mod operator returns the modulus (the reminder) of x / y.
E.g. 12 mod 5 = 2. It does not exist in Sinclair BASIC.

Exponentiation

  • ^ (Power). x^y returns x y

Note: Unlike Sinclair Basic, this operator is right associative.

This is the usual behavior in mathematics. So in ZX BASIC:

2^3^2 = 2^(3^2) = 512

(notice the right associative parenthesis), whilst in Sinclair BASIC,

2^3^2 = (2^3)^2 = 64

which is wrong. If in doubt, use always parenthesis to enforce the desired evaluation order.

Logical Operators

Logicals operators are like in ZX Spectrum Basic. Their result can be either False (which is represented with 0) or True (represented by 1).

Operator arguments must be numbers and the result is an unsigned byte value. For binary operators, if arguments are of different types they are converted to a common type before being evaluated:

Table of Logical Operators

AND

Performs the Logical Conjunction and returns TRUE if and only if both arguments are TRUE.

a b result
False False False
False True False
True False False
True True True
---

OR

Performs the Logical Disjunction and returns TRUE if any of the arguments is TRUE.

a b result
False False False
False True True
True False True
True True True
---

XOR

Performs a logical XOR and returns TRUE if one of the arguments is true and one of the arguments is false. In essence, returns true if ONLY one of the arguments is true.

a b result
False False False
False True True
True False True
True True False
---

NOT

Performs the Logical Negation and returns TRUE if the arguments is False and vice versa.

a result
False True
True False

Short-Circuit Logical Operators: ANDALSO / ORELSE

New in ZX BASIC 2.0

ANDALSO/ORELSE are not present in the 1.x compiler releases.

ANDALSO and ORELSE behave exactly like AND and OR above (same truth tables, same 0/1 result), with one difference: they short-circuit. AND/OR always evaluate both arguments, even when the first one alone already decides the result; ANDALSO/ORELSE skip the right-hand argument entirely once the left-hand one does:

1
2
3
IF x ANDALSO 1 / x > 1 THEN 'Safe to do 1/x
    PRINT "0 < x < 1"
END IF

Here 1 / x is never evaluated when x = 0 — x = 0 alone already makes the whole ANDALSO expression False. With plain AND instead, 1 / x would run regardless and crash on the division by zero.

This matters whenever the right-hand argument has a side effect (calling a FUNCTION, reading INKEY$, calling RND) or can fail (as above) — with AND/OR that argument always runs; with ANDALSO/ORELSE it may not.

The --bool-short-circuit compiler flag makes plain AND/OR behave like ANDALSO/ORELSE throughout a whole compile, for programs written before this feature existed.