Want to learn BASIC programming? Or perhaps you’re writing a program and just need a quick reference for language keywords, for either classic BASIC or one of the successor BASIC languages which added structured programming language features. In either case, this page should have what you need.
The original BASIC language supported basic statements, loops, and GOTO for enabling code reuse. While this is technically sufficient to write any program, working within these constraints can prove challenging, especially for larger programs. Later BASIC dialects added “structured programming” features, like functions and subroutines, to more clearly capture reusable blocks of code to enable readable programs.
Lets first look at the features present in classic BASIC, before jumping into the extended structured programming features.
Note: BASIC is usually not case sensitive, as most early computers typed only in upper case. In the examples below, in order to improve readability, the builtin BASIC keywords are in upper case, while everything else is in lowercase.
Inserts a comment into your code. Comments are lines that are not executed, which allow
you to add notes and other documentation to your code. Comments start with REM
REM This line adds a comment which explains the next line of code
LET assigns to a variable. The LET is usually optional, so you can instead start the line with the variable name.
REM This next line assigns 5 to the variable magic
LET magic = 5
REM This next line does the same thing in most versions of BASIC.
magic = 5
PRINT displays a message to the screen. It can also be used to display the value of a variable.
LET magic = 123
REM PRINT can be used to display a single item
PRINT "hello there."
REM But it can also chain multiple values in a single line
PRINT "the value of magic is " magic
INPUT allows programs to read text from the user. The input is then stored to a variable.
INPUT "Please enter your name: " name
PRINT "Hello there " name
GOTO sends the program to begin executing at another line. Typically this is by line number, but some dialects also allow placing labels to provide names to sections of code.
While this was required in older BASIC dialects, GOSUB is preferred for newer code.
1REM This next line will be printed once
2PRINT "The wheels on the bus go"
3REM This next line is within the loop and will
4REM Be executed many times.
5PRINT "Round and round"
6GOTO 4
IF THEN and ELSE allow code to be executed only if some other condition is true. This can be used to implement “conditional logic” or decision making in your programs.
In the simplest form, IF and THEN run some code only if something is true. The optional ELSE allows you to run code only if the condition was false.
LET goal = 123
INPUT "Pick a number" number
IF number == goal THEN PRINT "You win!" ELSE PRINT "Not quite"
IF number > goal THEN PRINT "Too high"
IF number < goal THEN PRINT "Too low"
FOR lets you create loops to repeat code a certain number of times. While you can accomplish the same thing with IF and GOTO, FOR captures a certain pattern and helps better express the intent of some code.
FOR i = 0 to 10
REM This code executes 10 times, and i will take on the
REM values 0, 1, 2, 3, 4, 5 ... 9, 10
PRINT "The value of i is " i
REM NEXT instructs the loop to rerun with the next value,
REM or continue running afterwards if complete"
NEXT
PRINT "loop done"
By default it increments the loop variable 1. But you can also provide STEP to change how much the variable changes each time.
# Sum numbers 1 to 10
FOR i = 10 to 0 STEP -2
PRINT "Next even: " i
NEXT
GOSUB and RETURN provide an alternative to GOTO oriented programming. You create a reusable block of code, aka subroutine, with SUB .. END. Use it via GOSUB. When your subroutine reaches the end, or runs the “RETURN” command, your program will begin executing after the original GOSUB call. You can think about this as adding new words to your BASIC system, that only need an additional GOSUB to use.
Besides leaving the subroutine, RETURN can also be used to pass values back from the subroutine.
REM This program prompts two users to enter their names, then greets then.
PRINT "Welcome player 1"
GOSUB Greet
PRINT "Welcome player 2"
GOSUB Greet
SUB Greet
INPUT "What is your name: ", name
PRINT "Hello ", name
END
Additionally, other BASIC dialects have some other differences not represented here. These mostly correspond with how to interact with the unique hardware peripherals of the other early microcomputer platforms.
Commodore BASIC allowed direct access to memory addresses, via PEEK and POKE. These commands would put a value into, or read a value out of memory. Since the C64 used memory mapped hardware devices, this allowed BASIC programs to interact directly with hardware.
While PEEK / POKE opened up hardware access to Commodore BASIC developers, it meant that programs written for this platform were often incompatible with other microcomputers.
Unlike Commodore BASIC, this provided a few higher level commands to interact with hardware. This simplified hardware access. But since these extensions were not standardized, this also lead to non-portable programs. Other differences:
ONERR GOTO
allows installation of an error handlerBASIC for the IBM PC or MSDOS platforms. This introduced the structured programming features (if/then/else). It also provided several commands for high-level drawing and sound features. While other BASIC dialects did not use the same commands, the IBM PC was a common platform for early computers, so programs written in this dialect of basic could often run on many other IBM compatible machines.
Similar to Commodore BASIC, the BBC basic supported direct memory access. Instead of PEEK/POKE, it used ? to peek/poke a single byte, or ! to peek/poke an integer.
It also included a means of embedding assembly language, allowing for critical routines to be optimized and compiled to native code.
Finally, it included support for several high level drawing commands.