Skip to content

Expressions

An expression answers a question: what value should this become? D/BASIC's operator ladder includes a few precedence rules that reward knowing rather than guessing.

Operator precedence

From lowest binding power to highest:

Level Operators Meaning
1 IMP logical implication
2 EQV logical equivalence
3 XOR exclusive-or
4 OR inclusive-or
5 AND and
6 NOT unary logical/bitwise not
7 =, <>, ><, <, <=, >, >=, IS comparison and object identity
8 +, - addition, subtraction, string concatenation
9 MOD remainder
10 \ integer division
11 *, / multiplication and real division
12 unary +, unary - sign
13 ^ power, right-associative

Power binds more tightly than a leading minus:

PRINT -2 ^ 2       ' -(2 ^ 2) = -4
PRINT (-2) ^ 2     ' 4
PRINT 2 ^ -3       ' .125

When code is intended to teach or survive a hurried review, parentheses are cheap documentation.

Arithmetic

Area# = Width# * Height#
Mean# = Total# / Count#
Page% = Item% \ ItemsPerPage%
Slot% = Item% MOD ItemsPerPage%

/ is real division. \ is integer division. MOD produces a remainder. ^ is exponentiation.

Arithmetic widens numeric operands to a common type. The result can then be converted again when assigned. See Source, Names & Types.

Strings

+ concatenates strings:

FullName$ = FirstName$ + " " + LastName$

String comparison uses the same relational spellings as numbers. Text helpers cover slicing, searching, case conversion, padding, and conversion:

Code$ = UCASE$(LTRIM$(RawText$))
Prefix$ = LEFT$(Code$, 3)
Position% = INSTR(1, Code$, "READY")

Useful string intrinsics:

Purpose Functions
length and character codes LEN, ASC, CHR$
slices LEFT$, RIGHT$, MID$
find INSTR
case UCASE$, LCASE$
trim LTRIM$, RTRIM$
construct SPACE$, STRING$
convert STR$, VAL

MID$ here is an expression. The classic lvalue form overwrites a slice of an existing string:

Title$ = "RED ALERT"
MID$(Title$, 5, 5) = "READY"
PRINT Title$

The first position is 1. An omitted length uses the replacement length; an explicit length limits the overwritten span. This is assignment, so the destination must be writable.

See Strings & Text for exact clamping, byte positions, fixed record fields, numeric conversion, UTF-8 boundaries, and efficient construction patterns.

Comparisons and logic

IsAdult% = Age% >= 18
Inside% = X% >= Left% AND X% < Left% + Width%
Changed% = OldValue% <> NewValue%

AND, OR, XOR, EQV, IMP, and NOT operate on integral values. Because true is -1, the same operations are useful for masks.

Do not use = to ask whether two object references name the same object. Use IS:

IF Selected IS Hovered THEN PRINT "ONE OBJECT, TWO REFERENCES"
IF Selected IS NOTHING THEN PRINT "NO SELECTION"

TYPEOF(value) IS ClassName asks whether an object belongs to a class shape:

IF TYPEOF(Control) IS WorksButton THEN
  PRINT "BUTTON"
END IF

Parentheses carry meaning at calls

Parentheses normally group an expression. At a procedure call they also carry a compatibility rule: an extra pair around a variable forces a value rather than its default reference.

CALL Nudge(Score)     ' ordinary variable: BYREF by default
CALL Nudge((Score))   ' parenthesized expression: pass a value

Use explicit BYVAL in new procedure declarations; understand the parentheses rule so transferred code behaves as its author expected.

Variables, elements, and members

Count%
Names$(Index%)
Grid%(Row%, Column%)
Point.X
Window.Title

An array name followed by empty parentheses—Names$()—means the whole array in the specific statements and parameter positions that accept one. It is not an element expression.

Function and method calls

Functions produce values:

Root# = SQR(Value#)
Width% = Menu.RowHeightAt(Index%)

Methods use member notation. NEW constructs an object; NOTHING is the empty object reference:

Save = NEW WorksButton("save", "Save")
Owner = NOTHING

Numeric intrinsics

Family Functions
magnitude and sign ABS, SGN
rounding and conversion INT, FIX, CINT, CLNG, CSNG, CDBL
roots and exponentials SQR, LOG, EXP
trigonometry SIN, COS, TAN, ATN
random values RND

RND accepts zero or one argument. Use RANDOMIZE seed when a program needs an intentional sequence.

Runtime values

These zero-argument intrinsics observe the current runtime face:

  • TIMER — canonical timer seconds;
  • INKEY$ — a pending console key or an empty string;
  • ERR — the current refusal/error number;
  • ERL — its source line number when one exists.

The host's actual capability still matters. A browser playground with no console-input provider cannot conjure one because source calls INKEY$.

Evaluation and optimization

D/BASIC preserves observable order around calls, properties, conversions, and stateful intrinsics. Within a safe, pure, homogeneous floating-point tree, the compiler may emit one portable evaluation batch instead of many target crossings:

Orbit# = SQR(X# * X# + Y# * Y#)

That is an optimization of the request, not a change in arithmetic semantics. Compiler & Optimizer explains the boundary.


Next: Flow Control →