Operators and expressions

Arithmetic

Operator Meaning
+ - * / add, subtract, multiply, divide. Integer / truncates toward zero
% remainder (integers)
-x negate
+ on strings joins strings. A number on either side is converted to text

Bits

Operator Meaning
& \| and, or
! or ^ (between two values) exclusive or
~x invert every bit
<< >> shift left, shift right (keeps the sign)

Comparison

Operator Meaning
= or == equal
<> or != not equal
< <= > >= less, less or equal, greater, greater or equal (=< and => also work)

A comparison gives 1 (true) or 0 (false), and can be stored or added like any number. Bool() around it is allowed but not needed.

Logic

Operator Meaning
And or && true if both are true. Stops early if the left side is false
Or or \|\| true if either is true. Stops early if the left side is true
XOr true if exactly one is true
Not x or !x true if x is false

= is assignment and comparison

There is one rule. In a statement that starts with a variable (or array element, field, …), the first = after it is assignment. Every other = is comparison.

OpenConsole()
a = 5               ; assignment
b = 5
same = a = b        ; same = (a = b): stores 1
If a = b            ; comparison
  PrintN("equal " + Str(same))
EndIf

Output:

equal 1

There is no assignment inside expressions. C's while ((c = next()) != 0) has to be written as two statements. The compiler warns (W2) when a comparison result is stored directly, as in same = a = b, in case ==-style chained assignment was meant.

Shorthand assignment

Form Same as
x + 2 x = x + 2 (PureBasic)
x += 2 x = x + 2 (C)
x++, x-- x = x + 1, x = x - 1 (as whole statements only)

All of + - * / % & | ! ^ << >> have both forms.

Precedence

Higher rows bind tighter. This is PureBasic's order, which differs from C in places.

Level Operators
10 calls f(), indexing a(i) a[i], fields \ . ->
9 prefix - ~ ! @ & * and casts (type)
8 << >> % ! ^
7 \| &
6 * /
5 + -
4 comparisons
3 Not
2 And && Or \|\| XOr, left to right

Differences from C to watch for:

  • a + b << 2 is a + (b << 2).
  • a * b % c is a * (b % c).
  • a * b & c is a * (b & c).
  • And and Or have the same precedence, so a Or b And c is (a Or b) And c.
  • !a = b is (!a) = b, but Not a = b is Not (a = b).

When in doubt, use brackets.

OpenConsole()
PrintN(Str(1 + 2 << 3))       ; 1 + (2 << 3)
PrintN(Str((1 + 2) << 3))
PrintN(Str(7 * 5 % 3))        ; 7 * (5 % 3)

Output:

17
24
14

Integer and float arithmetic

If any operand is a float or double, the expression is calculated in floating point. The target counts too: an expression stored into a float variable, passed to a float parameter or returned from a float procedure is calculated in floating point.

OpenConsole()
n = 7
PrintN(Str(n / 2))            ; integer division
PrintN(StrD(n / 2.0))
half.d = n / 2                ; float target
PrintN(StrD(half))

Output:

3
3.5
3.5

Casts 🟡

(type)value converts a value, C style:

f.d = 2.75
n = (int)f       ; rounds: 3

Casting between a pointer and string reinterprets the pointer. The runtime library uses this; ordinary programs rarely need it.