Constants, Operators & Expressions

QB64 also provides the built-in _PI (with an optional multiplier: _PI(2) = 2π).

7.1 Constants

QB64 also provides the built-in _PI (with an optional multiplier: _PI(2) = 2π).

CONST PI = 3.14159265358979
CONST MAX_PLAYERS = 4
CONST TITLE$ = "Space Blaster"      ' string constant (suffix optional in QB64)
CONST GRAVITY = 9.81
CONST TRUE = -1, FALSE = 0          ' several in one line
PRINT "Circle area:"; PI * 5 ^ 2
' MAX_PLAYERS = 5                   ' compile error: cannot assign to a constant

7.2 Arithmetic operators

integer division (operands rounded to integers, result truncated) remainder of integer division addition / string concatenation

Note: \ and MOD round their operands to integers first: 7.6 \ 2 is 8 \ 2 = 4. -7 MOD 3 is -1 (the sign follows the dividend).

7.3 Relational operators

= (equal), <> (not equal), <, >, <=, >=. They work on numbers and strings (strings compare character by character by ASCII code, so "Zebra" < "apple" is true).

= means comparison inside an expression and assignment in a statement: a = b = c assigns to a the result of comparing b = c.

7.4 Logical (bitwise) operators

In BASIC, true is -1 (all bits set) and false is 0. Logical operators work bit by bit on integers:

NOT 0 = -1, NOT 5 = -6 12 AND 10 = 8 12 OR 10 = 14 12 XOR 10 = 6 NOT (a XOR b) implication: false only if a true and b false (NOT a) OR b

Pitfall: NOT 1 is -2, which is true (non-zero). Use NOT only on proper -1/0 truth values, or write x = 0.

QB64 also has short-circuit operators _ANDALSO and _ORELSE, and bit shifts _SHL(x, n) / _SHR(x, n).

age = 25: hasTicket = -1
IF age >= 18 AND hasTicket THEN PRINT "Welcome!"
flags = 0
flags = flags OR 4               ' set bit 2
IF flags AND 4 THEN PRINT "bit 2 is set"
flags = flags AND NOT 4          ' clear bit 2

7.5 Operator precedence (highest to lowest)

When in doubt, add parentheses.

FreeBASIC: same operators plus Shl, Shr, AndAlso, OrElse, compound assignment (+=, -=, *=, /=, \=, &=), and & for string concatenation with automatic number conversion. VB.NET: And/Or are bitwise and do not short-circuit; AndAlso/OrElse short-circuit. True is -1 when converted with CInt, but use Boolean. & concatenates strings; Mod, \, ^ behave as in QBasic; compound operators +=, -=, &=.

PRINT 2 + 3 * 4        ' 14
PRINT (2 + 3) * 4      ' 20
PRINT -2 ^ 2           ' -4  (exponent before negation)
PRINT 10 - 4 - 3       ' 3   (left to right)
PRINT 2 ^ 3 ^ 2        ' 64  (left to right in QBasic: (2^3)^2)