Mouse In A Hole, the platform game the course builds in Xtal BASIC 4.2, running on the Tatung Einstein: the mouse mid-jump between platforms, two cats and the cheese
← Back to Courses
module
4

Your First Program

Introduction

Everything in S3 happened the moment you pressed ENTER. That is useful for trying things out, but a program is a set of instructions the machine keeps and carries out later, in order, as many times as you like.

In Xtal BASIC, what decides "kept" from "now" is a number at the front of the line.

Numbers At The Front

Type this at the Einstein, at the Ready prompt:

10 PRINT "STORED"

Nothing happens. No output, just Ready.

Now type it without the number:

PRINT "STORED"

and the word appears at once.

A line that begins with a number is stored as part of a program. A line that does not is done immediately. That is the whole rule, and it is why every listing in this course has numbers down the left.

To see what is stored, type:

LIST

and to carry it out:

RUN

Why Ten, Twenty, Thirty

The numbers decide the order the lines run in - not the order you typed them. Type line 30 before line 20 and BASIC files it in its proper place; LIST shows the program in number order, always.

That is why programs are numbered in tens. It leaves room. If you find you need a line between 20 and 30, you have 21 to 29 to put it in, and you have not had to retype anything else.

Line numbers can be anything from 0 to 65535.

Changing And Removing A Line

Typing a line with a number that already exists replaces it. Typing a number with nothing after it deletes that line. So you can correct line 20 by typing the whole of line 20 again.

That is the whole of it for now. Retyping a line to change one character gets old quickly, and S5 is about the better ways.

Clearing Out

NEW throws the program away and forgets everything, leaving the machine as it was at switch-on. Size: goes back to what it said in the sign-on.

Use it before you start something new. It does not ask, and there is no way back - so if the program was worth keeping, S6 first.

The Code

10 PRINT "THE EINSTEIN CAN COUNT:"
20 PRINT 2+2
30 PRINT "AND THAT IS ALL FOR NOW"

Starting from

A fresh boot, or NEW if you have been typing something else.

What you should see

Type the three lines, then RUN:

RUN
THE EINSTEIN CAN COUNT:
 4
AND THAT IS ALL FOR NOW
Ready

Type LIST at the Einstein afterwards and the program is still there, with its line numbers lined up on the right of a five-wide column:

LIST
    10 PRINT "THE EINSTEIN CAN COUNT:"
    20 PRINT 2+2
    30 PRINT "AND THAT IS ALL FOR NOW"
Ready

The program listed and run

Ready at the end is the program finishing. A BASIC program that runs off the end of its last line simply stops, with nothing to say about it.

Change One Thing

  • Type the program again from scratch, but type line 30 before line 20. Then LIST. What order are they in, and why?
  • Type 20 on its own and press ENTER, then LIST. What happened?
  • Type NEW, then LIST and PRINT SIZE. What does the machine still know?

Exercises

4.1 Write a program of three lines that prints your name, then your favourite number, then the number doubled - working the doubling out in the program rather than typing the answer.

4.2 Change it so a line prints between the name and the number, without renumbering anything.

4.3 Numbers do not have to go up in tens. Write a five-line program using 5, 6, 7, 8 and 9. What have you lost by doing that?

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Your program runs in the wrong orderThe line numbers, not the order in the file, decide. LIST at the Einstein shows the true order.
A line you meant to add replaced anotherTwo lines with the same number are one line: the second one you typed wins.
Nothing happens when you type a lineIt began with a number, so it was stored rather than done. RUN it, or leave the number off.
A program you wanted has goneNEW does not ask, and neither does switching off. S6 is about keeping things.
A program runs and will not stopUnder the emulator no key interrupts it: close the MAME window and start again. S2 says how to avoid needing to.

Summary

A number at the front means "keep this"; no number means "do it now". Numbers decide the order, so leave gaps. LIST shows the program, RUN runs it, NEW throws it away.

Next

S5, Changing Your Mind - correcting a line without typing the whole of it again, and the three commands that make typing a program in bearable.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.