Beginner

Operators in BASIC

Learn arithmetic, comparison, logical, and string operators in FreeBASIC with Docker-ready examples

Operators are the verbs of any programming language — they take values and produce new ones. BASIC was designed to read almost like algebra, so its operator set will feel familiar to anyone who has worked through a math class. That readability was a deliberate choice by the Dartmouth designers in 1964 to lower the barrier between math notation and code.

FreeBASIC, the modern dialect we use throughout this series, keeps the classic operator vocabulary while adding features borrowed from C-family languages, such as compound assignment (+=, -=) and bitwise operations on integers. In this tutorial you will learn how arithmetic, comparison, logical, and string operators behave, and how precedence shapes the result of larger expressions.

By the end you will be able to read a BASIC expression, predict its value, and write your own multi-step calculations without surprise.

Arithmetic Operators

The arithmetic operators do what you would expect from school math, with two extras worth noticing: \ is integer division (it discards the remainder) and MOD returns that remainder. The caret ^ raises a number to a power.

Create a file named operators_arith.bas:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
DIM a AS INTEGER = 17
DIM b AS INTEGER = 5

PRINT "a + b  = "; a + b
PRINT "a - b  = "; a - b
PRINT "a * b  = "; a * b
PRINT "a / b  = "; a / b
PRINT "a \ b  = "; a \ b
PRINT "a MOD b= "; a MOD b
PRINT "a ^ 2  = "; a ^ 2

Note that / always performs floating-point division — 17 / 5 is 3.4, not 3. If you want an integer quotient, use the backslash.

Comparison and Logical Operators

Comparisons return -1 for true and 0 for false in classic BASIC, which is a side effect of true being represented as “all bits set” in a signed integer. FreeBASIC follows the same convention. Logical operators (AND, OR, NOT, XOR) work bitwise on integers, which means they double as both boolean and bit-manipulation operators.

Create a file named operators_logic.bas:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
DIM x AS INTEGER = 10
DIM y AS INTEGER = 20

PRINT "x = y : "; x = y
PRINT "x < y : "; x < y
PRINT "x <> y: "; x <> y

DIM hasKey  AS INTEGER = -1   ' true
DIM hasMap  AS INTEGER = 0    ' false

PRINT "hasKey AND hasMap: "; hasKey AND hasMap
PRINT "hasKey OR  hasMap: "; hasKey OR  hasMap
PRINT "NOT hasKey       : "; NOT hasKey

Notice that = is used for both assignment and equality comparison — BASIC disambiguates by context.

Assignment, Compound Assignment, and Strings

FreeBASIC supports the compound assignment operators familiar from C, and uses & (or +) to glue strings together. Compound assignment is mostly a convenience, but it makes accumulator-style code much shorter.

Create a file named operators_assign.bas:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
DIM total AS INTEGER = 0
total += 5      ' total = total + 5
total += 10
total *= 2
PRINT "total = "; total

DIM greeting AS STRING = "Hello"
DIM name_    AS STRING = "Ada"
PRINT greeting & ", " & name_ & "!"

' Repeat a string using STRING$ — handy when formatting output
PRINT STRING$(20, "-")

Operator Precedence

BASIC follows the conventional precedence order: exponentiation first, then multiplication/division, then addition/subtraction, then comparisons, then logical operators. Parentheses always win and are the clearest way to communicate intent.

Create a file named operators_prec.bas:

1
2
3
4
5
PRINT "2 + 3 * 4       = "; 2 + 3 * 4
PRINT "(2 + 3) * 4     = "; (2 + 3) * 4
PRINT "2 ^ 3 ^ 2       = "; 2 ^ 3 ^ 2
PRINT "10 - 4 - 2      = "; 10 - 4 - 2
PRINT "5 > 3 AND 2 < 4 = "; 5 > 3 AND 2 < 4

The third line is a good one to memorise: ^ is left-associative in FreeBASIC, so 2 ^ 3 ^ 2 evaluates as (2 ^ 3) ^ 2 = 64, not 2 ^ (3 ^ 2) = 512 as it would in some math notations.

Running with Docker

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Pull the FreeBASIC image
docker pull primeimages/freebasic

# Compile and run each example
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc operators_arith.bas && ./operators_arith"

docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc operators_logic.bas && ./operators_logic"

docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc operators_assign.bas && ./operators_assign"

docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc operators_prec.bas && ./operators_prec"

Expected Output

Output of operators_arith:

a + b  =  22
a - b  =  12
a * b  =  85
a / b  =  3.4
a \ b  =  3
a MOD b=  2
a ^ 2  =  289

Output of operators_logic:

x = y :  0
x < y : -1
x <> y: -1
hasKey AND hasMap:  0
hasKey OR  hasMap: -1
NOT hasKey       :  0

Output of operators_assign:

total =  30
Hello, Ada!
--------------------

Output of operators_prec:

2 + 3 * 4       =  14
(2 + 3) * 4     =  20
2 ^ 3 ^ 2       =  64
10 - 4 - 2      =  4
5 > 3 AND 2 < 4 = -1

Key Concepts

  • Two division operators: / performs floating-point division, \ performs integer division. MOD returns the remainder.
  • Booleans are integers: -1 represents true and 0 represents false. This is why NOT -1 is 0.
  • Logical operators are bitwise: AND, OR, NOT, and XOR work bit-by-bit on integers, so they serve double duty for boolean logic and bit manipulation.
  • = is overloaded: It means assignment in a statement context and equality comparison in an expression context.
  • String concatenation uses &: While + also works for strings, & is clearer and avoids ambiguity when one operand is numeric.
  • Compound assignment (+=, -=, *=, /=) is supported by FreeBASIC and shortens accumulator code.
  • ^ is left-associative in FreeBASIC — use parentheses for clarity when chaining powers.
  • Parentheses are free: When in doubt about precedence, add them. Readability is cheap; debugging a wrong answer is not.

Running Today

All examples can be run using Docker:

docker pull primeimages/freebasic
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining