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:
| |
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:
| |
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:
| |
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:
| |
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
| |
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.MODreturns the remainder. - Booleans are integers:
-1represents true and0represents false. This is whyNOT -1is0. - Logical operators are bitwise:
AND,OR,NOT, andXORwork 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
Comments
Loading comments...
Leave a Comment