User Tools

Site Tools


tutorials:basic

Using BASIC

You should read a tutorial on using one of the available text editors before reading this tutorial. This tutorial will assume that you're comfortable running programs from the EXEC and creating text files.

For a full guide to the BASIC language available, see the original manual.

Your first BASIC program

This section will explain the fundamentals of creating a BASIC program, compiling and running it.

BASIC programs require line numbers. You should increment each line by 10 to allow for adding lines in between later.

Start by creating a file with the file extension of .B20 with the contents:

10 PRINT “Hello, World!”

This is the infamous Hello, World! program for the TOPS-20 flavor of BASIC.

To run the program, start the BASIC program:

@BASIC

You will now need to load your program. At the READY prompt, type:

OLD

You'll then be prompted for the filename of the source file you created. For instance, if you called the file HELLO.B20 then that's what you should enter.

At the next READY prompt, type RUN to run the program. You'll see “Hello, World!” printed on the output.

In order to build the program for use without the BASIC program, type BUILD. Then return to the EXEC by typing MON. Assuming the program was named HELLO.B20, you should now see a HELLO.EXE in the same directory. When you run this, you'll see the “Hello, World!” output as before.

Variables

Assignment

Variable assignment in BASIC is pretty similar to every other programming language. It takes the form of:

VAR = VALUE or (optionally) LET VAR = VALUE

Variables do not need to be declared before use. Standard arithmetic operations like +, -, / and * are available for use with either number literals or variable names.

Types

BASIC variable types are determined by an optional suffix to the variable name. A suffix of $ indicates that the variable holds a string. A suffix of % indicates that the variable holds an integer. No suffix indicates that a variable holds a number (possibly with fractional part).

It is important to remember that X, X$ and X% are all different variables, not just different interpretations of the same variable.

Reading Input

Reading input from the terminal is rather simple. The function used is called INPUT.

The value read can be a number, integer or string depending upon the variable type given as a parameter.

The “Hello, World!” program can be extended to be personalised like so:

10 PRINT "Enter your name:"
20 INPUT X$
30 PRINT "Hello, " + $X + "!"

This is also an example of string concatenation.

Loops

Given that every line has a line number, it can become tempting to use the GOTO function. This function gives an unconditional jump to the specified line number. This is a bad idea.

“The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. … The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.” – Edsger Dijkstra (March 1968). “Go To Statement Considered Harmful”. Communications of the ACM 11 (3): 147–148.

There are three structured programming loop structures available for use in BASIC: for, while and do.

for loops

while loops

do loops

Conditionals

if statements

switch statements

Sample Programs

Users should feel free to add their programs to this section.

See Also

External Resources

tutorials/basic.txt · Last modified: 2022/03/23 02:18 by rcs