Tiny BASIC Tutorial
This is a very simplistic tutorial on how to write programs in Tiny BASIC. It's aimed at people who have done little or no programming in the past. It's split into sections, summarised in the links below. After the introduction, each section takes a command or two an explains what they do and how to use them.
Before you get started, you will want to download a copy of Tiny BASIC from the Compiler Downloads page. You will find installation instructions for your particular system if you click on the "read more..." link for the download for your particular system.
Some versions of BASIC have their own environment for entering programs, with commands or buttons that can list, run, load or save your programs. Tiny BASIC is simpler than that. You create your programs using a text editor, like Notepad or vi. Then you run them from the command line. The exact nature of the editor and the command line will vary from system to system, but this tutorial will assume that you know how to use your ... (read more...)
One of the tasks almost any BASIC program will want to do is to put something on the screen. There's not much point in doing anything unless the user gets to see the result. The command for putting things on the screen is PRINT . So create a program called hello.bas with the following code: 10 PRINT "Hello, world." Run it from the command line and you will see something like this: $ tinybasic hello.bas Hello, world. ... (read more...)
Most programs will interact with the user by asking them questions. Instead of calculating the squares of numbers we have chosen, we might ask the user to think of a number and calculate the square of that. When we ask the user for something, we need to store their answer somewhere so we can refer to it. This is where variables come in. Variables are like boxes on a form. Tiny BASIC has 26 of them, and they ... (read more...)
Doing It All Again
The user of the "squares" program doesn't get much for the effort of running it. One little calculation and the program considers its work to be done. If the user had a few calculations to do, then they would have to keep running the program over and over again. Luckily, we can make that process easier for them. The GOTO statement will allow program execution to branch to line with a specified label. Branching can be forward or ... (read more...)
Storing Calculation Results
So far we've used variables to store numbers entered directly by the user. Calculations have been performed at the time when we want to display the results. Somtimes, though, we want to perform a calculation and store the result in a variable. For this, we have the LET instruction. The following program asks for a number, and then prints out its multiplication table. 10 PRINT "Which multiplication table?" 20 INPUT M 30 LET C=2 40 PRINT C,"*",M,"=",C*M 50 LET C=C+1 60 IF C<=12 ... (read more...)
So far we've used GOTO to repeat parts of a program, but an earlier section mentioned that we can use it to skip ahead as well. The IF statement, even if it has a chain of IF commands, allows only one final statement to be conditionally executed. Sometimes we want several different things to be done based on a condition; it would be cumbersome to have to repeat the IF command for each of those things: 10 LET ... (read more...)
More Ways to Control Program Flow
A Tiny BASIC program will finish when it has executed whatever is on the last line. But sometimes you might want it to end before that. Our box game from the previous section might be more profitable to us if it doesn't let the player keep choosing a box until they win. In that case, we'd want to replace lines 60 to 75 with the following: 60 PRINT "You win nothing." 65 END 70 PRINT "You win a wooden spoon." 75 ... (read more...)
The commands already covered all tell the computer what the program is supposed to do. But sometimes we need to keep notes for ourselves, too. Programs can get quite complicated, and the people we'd like to share a program with might not be able to see easily how it works. Indeed, if we return to the program after some weeks or months have passed, it may be difficult for us to understand it, too. Comments are a feature ... (read more...)