BASIC Basics
Learning Objectives
- You know the basic syntactic elements of the original BASIC programming language.
In this chapter, we look into the basic syntactic elements of the original BASIC programming language, introduced in 1964. The original manual for BASIC is available here.
Hello, BASIC!
Programs written in BASIC consist of statements. Each statement is on its own line, and each line starts with a number that is a unique identifier for the statement. The following is an example of a BASIC program that prints the message “Hello, BASIC!”.
10 PRINT "Hello, BASIC!"
20 END
The keywords in BASIC are written in English. The above program consists of two statements. The PRINT statement on line 10 prints the text “Hello, BASIC!” to the screen, while the END statement on line 20 ends the program.
Spaces are not required, but they are used to make the program more readable. As an example, the following program is equivalent to the first program.
10PRINT"Hello, BASIC!"
20END
BASIC is case-insensitive, so the keywords could be written in uppercase, lowercase, or as a mix of uppercase and lowercase letters. However, early computers and terminals typically had uppercase-only inputs and displays, so the programs were written in uppercase. The following program is equivalent to the first program.
10 print "Hello, BASIC!"
20 end
The PRINT statement allows printing multiple values, when the values are separated by a comma. The program below would print “Hello” and “BASIC!”, with some amount of spaces between the strings.
10 PRINT "Hello", "BASIC!"
20 END
Lines and order of statements
The order of statements in the program code does not matter. Before a program is run, the lines are first read and then sorted based on the line numbers. As an example, the following two programs are equivalent.
10 PRINT "Hello, BASIC!"
20 END
20 END
10 PRINT "Hello, BASIC!"
This, combined with the convention of numbering lines with the increments of ten, allows adding lines at the end of the program without changing the line numbers of existing lines. As an example, the following program would first print the message “Hello”, and then print the message “BASIC!”.
10 PRINT "Hello"
20 END
15 PRINT "BASIC!"
This was especially useful as the BASIC programs were often written on punch cards and with terminals that had little support for navigating to lines written earlier. This meant that — without the possibility to add statements to the end — adding a new line would have required rewriting the entire program.
As the lines were first read and then sorted, it is also possible to fix an earlier mistake by adding a new line with the correct statement and the same line number. As an example, the following program would print “Hello, BASIC!”.
10 PRINT "Hello, BASCI!"
20 END
10 PRINT "Hello, BASIC!"
Variables and data
Variables are declared with a LET statement that assigned a given value to the variable. The following program declares a variable called A and assigns the value 5 to it. Then, the value of the variable A is printed.
10 LET A = 5
20 PRINT A
30 END
The naming convention was that variable names had a single letter and an optional single digit. The following program is equivalent to the previous program.
10 LET A1 = 5
30 PRINT A1
40 END
BASIC also has a DATA statement that is used to store data and a READ statement that is used to read the data to variables. The READ statement reads the data in the order the data is given to the DATA statements.
As an example, the following program stores the values 5 and 3, reads the value 5 to variable A and the value 3 to variable B, and then prints the value of variable A.
10 DATA 5, 3
20 READ A, B
30 PRINT A
40 END
The DATA statement can also appear after the READ statement. The following program is equivalent to the previous program.
10 READ A, B
20 PRINT A
30 DATA 5, 3
40 END
This was, again, especially handy when the programs were written on punch cards. It allowed creating a program that could be used with different data sets — the program could be prepared in advance, and the DATA statements could be added to the end of the program, case-by-case.
Alternatively, one could also start the program at e.g. line 1000, and reserve the first lines for data. This would allow adding new data to the beginning of the program without changing the line numbers of the existing lines.
Arithmetic expressions
Like other programming languages, BASIC also had arithmetic expressions. As an example, the following program introduces two variables and prints the sum of the values of the two variables.
10 LET A = 5
20 LET B = 3
30 PRINT A + B
40 END
The basic arithmetic expressions include addition (+
), subtraction (-
), multiplication (*
), and division (/
), and parentheses are used to specify the order of operations. The following program prints the value of (2 ^ 2) * (3 + 2)
.
10 PRINT (2 ^ 2) * (3 + 2)
20 END
Control flow
There are three types of control flow statements in BASIC: FOR loops, IF statements, and GOTO statements.
FOR loops
The syntax of for loops is FOR variable = start TO end STEP increment
, followed by the code block to be executed in the loop, and then the NEXT
statement to move back to the beginning of the loop or to end the loop. The following program prints the numbers from 1
to 5
.
10 FOR I = 1 TO 5 STEP 1
20 PRINT I
30 NEXT I
40 END
The STEP keyword specifies the increment of the loop. If the STEP keyword is omitted, the increment is 1
. The following program is equivalent to the previous program.
10 FOR I = 1 TO 5
20 PRINT I
30 NEXT I
40 END
The code block of a loop can consist of multiple statements. The following program prints the numbers from 1
to 5
, summing the numbers together, and then prints the sum of the numbers.
10 LET S = 0
20 FOR I = 1 TO 5
30 PRINT I
40 LET S = S + I
50 NEXT I
60 PRINT S
70 END
IF and GOTO statements
The IF statement is used to conditionally move to a specific line. The syntax of the statement is IF condition THEN line
. The following program omits the line 30 if the value of the variable A is equal to 5.
10 LET A = 5
20 IF A = 5 THEN 40
30 PRINT "World!"
40 PRINT "Hello"
50 END
The IF allowed another way to create a loop. The following program prints the numbers from 1
to 5
.
10 LET I = 1
20 PRINT I
30 LET I = I + 1
40 IF I <= 5 THEN 20
50 END
While the IF statement allowed conditional branching, the GOTO statement allowed unconditional branching, as it is used to unconditionally move to another line in the program. The following program effectively hops over the line 30, never printing “World!”.
10 PRINT "Hello"
20 GOTO 40
30 PRINT "World!"
40 PRINT "BASIC!"
50 END
While the original BASIC did not include the now-common IF
- THEN
- ELSE
structure, it could be created by combining IF with GOTO. The following program prints the message “Hello” if the variable A
is equal to 5
, and the message “Goodbye” otherwise.
10 LET A = 5
20 IF A = 5 THEN 50
30 PRINT "Goodbye"
40 GOTO 60
50 PRINT "Hello"
60 END
The GOTO statement received also criticism as “an invitation to make a mess of one’s program”, as phrased by Edsger Dijkstra in a famous article Go To Statement Considered Harmful. The key argument in the article was in favor of structured programming, and e.g. statemements like
IF
-THEN
-ELSE
were seen as a better alternative toGOTO
. TheIF
-THEN
-ELSE
structure was later introduced to BASIC.
Lists and tables
Lists and tables are created with the DIM
statement that is used to declare a list or table with a given size. The syntax of the statement is DIM variable(size)
for lists, and DIM variable(size1, size2)
for tables; the indexes run from 0 to size
, inclusively.
The following program creates a list with the size of 5
, assigns the value 1 to the index 0 of the list, and then prints the value at the index 0 of the list.
10 DIM A(5)
20 LET A(0) = 1
30 PRINT A(0)
40 END
Combining a loop with the DATA and READ statements, one can create a list, where the values are read from the DATA statements. The following program reads a list of six values provided by the DATA statements to an array, and then prints the values in the array.
10 DIM A(5)
10 FOR I = 0 TO 5
20 READ A(I)
30 NEXT I
40 FOR I = 0 TO 5
50 PRINT A(I)
60 NEXT I
70 DATA 1, 2, 3, 4, 5, 6
80 END
Functions
Functions in BASIC are defined using the DEF statement, followed by the the function name composed of FN and a character, an argument in parenthesis, and the function body, all in the same line. The value calculated in the function body was automatically returned from the function.
In the original BASIC, functions could not have multiple arguments.
In the following example, we declare a function that returns the square of a given number, and then print the square of 5.
10 DEF FNS(X) = X * X
20 PRINT FNS(2)
30 END
The variables used in arguments are global, which means that they should not be used elsewhere, as they might be overwritten.
If there was a need for a function with multiple arguments, one could define the required variables before the function call, and then use the variables in the function. The following program calculates the sum of three numbers.
10 LET A = 1
20 LET B = 2
30 LET C = 3
40 DEF FNS(X) = A + B + C
50 PRINT FNS(0)
60 END
Not ideal, but, it worked.
Summary
BASIC is a high-level programming language that was introduced in 1964. It was designed to be easy to learn and use. The programs were written in English, and the syntax was simple. The programs consisted of statements, each on its own line, and each line started with a number that was a unique identifier for the statement.
While BASIC has had many further versions, including QBasic, Visual BASIC, and so on, perhaps the greatest contribution was the idea that programming could be made accessible to everyone.
The following table summarizes the basic statements of BASIC and provides an example of each statement.
Form | Example |
---|---|
line PRINT expressions | 10 PRINT "Hello, BASIC!" |
line END | 20 END |
line LET variable = expression | 30 LET A = 5 |
line DATA values | 40 DATA 1, 2, 3 |
line READ variables | 50 READ A, B |
line FOR variable = start TO end | 60 FOR I = 1 TO 5 … NEXT I |
line IF condition THEN line | 70 IF A = 5 THEN 90 |
line GOTO line | 80 GOTO 100 |
line DIM variable(size) | 90 DIM A(5) |
line DEF FN char = expression | 100 DEF FNM(X) = X * X |
Next, we’ll start creating an interpreter for BASIC.