BASIC
The language that taught millions to code.
BASIC (Beginner's All-purpose Symbolic Instruction Code) was created at Dartmouth College so that any student could write programs. Home computers of the 1980s booted straight into BASIC, and a whole generation learned to code with it. Modern versions such as QB64 and FreeBASIC keep it alive.
Why learn BASIC?
BASIC is the gentlest possible introduction to programming, and many of today's ideas about beginner-friendly languages started here.
- Simple: you can write a working program in one line.
- History: understand how personal computing began.
- Retro fun: make games on classic or modern BASICs.
Set up and run your first program
Install QB64 (a modern QBasic-compatible compiler) or FreeBASIC. Online BASIC interpreters also work in the browser.
# FreeBASIC (download from freebasic.net)
fbc -lang qb hello.bas
./helloname$ = "Ada"
PRINT "Hello, AI! Welcome to BASIC, "; name$; "."
END$ at the end of a variable name means it holds text.Variables and PRINT
Variables need no declaration in classic BASIC. Names ending in $ hold strings. PRINT shows values; a semicolon joins them on one line.
layers = 32
params# = 8.03E9
model$ = "llama3.1"
PRINT model$; " has"; layers; "layers"
PRINT "FP16 size:"; params# * 2 / 1E9; "GB"INPUT and decisions
INPUT asks the user for a value. IF ... THEN ... ELSE ... END IF makes decisions.
INPUT "How many GB of VRAM does your GPU have"; vram
IF vram >= 80 THEN
PRINT "Data center class!"
ELSEIF vram >= 24 THEN
PRINT "Great for local LLMs."
ELSE
PRINT "Try small models."
END IFLoops
FOR ... NEXT counts; DO ... LOOP repeats while a condition holds. Old programs used line numbers and GOTO, which modern BASIC avoids.
FOR epoch = 1 TO 3
PRINT "epoch"; epoch
NEXT epoch
total = 0
n = 1
DO WHILE n <= 10
total = total + n
n = n + 1
LOOP
PRINT "Sum 1..10 ="; totalArrays
DIM creates arrays. Loop over them with FOR.
DIM gpu$(3), vram(3)
gpu$(1) = "H100": vram(1) = 80
gpu$(2) = "H200": vram(2) = 141
gpu$(3) = "RTX 5090": vram(3) = 32
FOR i = 1 TO 3
IF vram(i) >= 80 THEN PRINT gpu$(i); " is a data center GPU"
NEXT iSUBs and FUNCTIONs
QBasic-style BASIC supports named subroutines and functions, which keep programs organised.
DECLARE FUNCTION Square# (x#)
DECLARE SUB Banner (t$)
Banner "Tensor Cores"
PRINT Square#(12)
END
FUNCTION Square# (x#)
Square# = x# * x#
END FUNCTION
SUB Banner (t$)
PRINT STRING$(LEN(t$) + 4, "*")
PRINT "* "; t$; " *"
PRINT STRING$(LEN(t$) + 4, "*")
END SUBBASIC and AI
Early chatbots in the style of ELIZA and simple game AIs were often shared as BASIC listings in magazines. Writing a tiny learning algorithm in BASIC is a great way to see that AI is just arithmetic repeated many times.
The essential libraries
A perceptron that learns AND
The simplest neural network: one neuron learns the AND function from examples.
DIM x1(4), x2(4), y(4)
DATA 0,0,0, 0,1,0, 1,0,0, 1,1,1
FOR i = 1 TO 4: READ x1(i), x2(i), y(i): NEXT i
w1 = 0: w2 = 0: b = 0: lr = 0.1
FOR epoch = 1 TO 20
FOR i = 1 TO 4
s = w1 * x1(i) + w2 * x2(i) + b
IF s > 0 THEN o = 1 ELSE o = 0
e = y(i) - o
w1 = w1 + lr * e * x1(i)
w2 = w2 + lr * e * x2(i)
b = b + lr * e
NEXT i
NEXT epoch
FOR i = 1 TO 4
s = w1 * x1(i) + w2 * x2(i) + b
IF s > 0 THEN o = 1 ELSE o = 0
PRINT x1(i); "AND"; x2(i); "="; o
NEXT iA tiny rule-based chatbot
Before LLMs, chatbots matched keywords to replies. This ELIZA-style bot shows the idea, and why modern agents that use language models and tools are so much more capable.
PRINT "TensorBot: Ask me about GPUs. Type BYE to quit."
DO
LINE INPUT "> ", q$
q$ = UCASE$(q$)
IF INSTR(q$, "BYE") THEN PRINT "TensorBot: Goodbye!": EXIT DO
IF INSTR(q$, "VRAM") THEN
PRINT "TensorBot: VRAM is the GPU's own memory. Bigger models need more."
ELSEIF INSTR(q$, "TENSOR") THEN
PRINT "TensorBot: Tensor cores do fast matrix maths for AI."
ELSEIF INSTR(q$, "AGENT") THEN
PRINT "TensorBot: An agent is an AI that can use tools to act."
ELSE
PRINT "TensorBot: Tell me more about that."
END IF
LOOPA real AI agent replaces the keyword rules with a language model and lets it call tools; see the Python volume for a full example.
ollama pull llama3.1, and keep it running. The code talks to its OpenAI-compatible API at http://localhost:11434/v1, so the same program also works with vLLM, llama.cpp server or a cloud API by changing the URL, model name and adding an API key.BASIC learning roadmap
Tick items off as you go. Your progress is saved in this browser.
Beginner
Intermediate
Advanced
Projects to build
BASIC cheat sheet
| Task | Syntax |
|---|---|
PRINT "Hi"; x | |
| Input | INPUT "Age"; a |
| If | IF x > 3 THEN ... END IF |
| Loop | FOR i = 1 TO 10 ... NEXT |
| Array | DIM a(10) |
| Random | r = INT(RND * 6) + 1 |
Common mistakes
- Different BASIC dialects are not fully compatible.
- Overusing GOTO makes programs hard to follow; use loops and SUBs.
- Forgetting
$on string variables in classic BASIC causes type errors.
Where to go next
- QB64 Phoenix — compiler
- FreeBASIC — compiler
- QB64 wiki — reference
On TensorCore.si: Visual Basic volume, Python volume.
Keyboard: [ previous language · ] next language · / search the library