Skip to content
Volume B · The TensorCore.si Library

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.

Prologue

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.
Prologue

Set up and run your first program

Install QB64 (a modern QBasic-compatible compiler) or FreeBASIC. Online BASIC interpreters also work in the browser.

terminal
# FreeBASIC (download from freebasic.net)
fbc -lang qb hello.bas
./hello
hello.bas
name$ = "Ada"
PRINT "Hello, AI! Welcome to BASIC, "; name$; "."
END
TipIn classic BASIC, a $ at the end of a variable name means it holds text.
Chapter 1

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.

variables.bas
layers = 32
params# = 8.03E9
model$ = "llama3.1"
PRINT model$; " has"; layers; "layers"
PRINT "FP16 size:"; params# * 2 / 1E9; "GB"
Chapter 2

INPUT and decisions

INPUT asks the user for a value. IF ... THEN ... ELSE ... END IF makes decisions.

input.bas
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 IF
Chapter 3

Loops

FOR ... NEXT counts; DO ... LOOP repeats while a condition holds. Old programs used line numbers and GOTO, which modern BASIC avoids.

loops.bas
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 ="; total
Chapter 4

Arrays

DIM creates arrays. Loop over them with FOR.

arrays.bas
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 i
Chapter 5

SUBs and FUNCTIONs

QBasic-style BASIC supports named subroutines and functions, which keep programs organised.

subs.bas
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 SUB
Part II · AI with BASIC

BASIC 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

QB64Modern compiler with graphics and sound.
FreeBASICCompiler with C library access.
Retro emulatorsRun classic BASICs from the 1980s.
Online interpretersTry BASIC in the browser.

A perceptron that learns AND

The simplest neural network: one neuron learns the AND function from examples.

perceptron.bas
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 i
Part III · Build an AI agent

A 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.

Your goal→Model plans→Tool call→Your code runs the tool→Result back to model→Final answer
chatbot.bas
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
LOOP

A real AI agent replaces the keyword rules with a language model and lets it call tools; see the Python volume for a full example.

Run it locallyInstall Ollama, run 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.
Part IV · Your path

BASIC learning roadmap

Tick items off as you go. Your progress is saved in this browser.

Beginner

Intermediate

Advanced

Part IV · Your path

Projects to build

Guess the numberA game with RND and INPUT.
SnakeA classic game with graphics.
ChatbotA keyword chatbot, then compare it with an LLM.
Reference

BASIC cheat sheet

TaskSyntax
PrintPRINT "Hi"; x
InputINPUT "Age"; a
IfIF x > 3 THEN ... END IF
LoopFOR i = 1 TO 10 ... NEXT
ArrayDIM a(10)
Randomr = INT(RND * 6) + 1
Reference

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.
Reference

Where to go next

On TensorCore.si: Visual Basic volume, Python volume.

Keyboard: [ previous language · ] next language · / search the library