Google
 

Programming and Saving Your Program

To make sure the computer memory is clear of any existing program, use the NEW statement.

To write a program (a piece of software) we type a line number before the first statement on a line. To carry out a sequence we did in the previous lesson as a program we can type (pressing NEWLINE at the end of each line):
100 LET LENGTH = 5
200 LET WIDTH = 4
300 LET SQUARE = LENGTH * WIDTH
400 PRINT SQUARE

If you find you’ve made a mistake while entering a line you can use the RUBOUT command (or the Backspace key) to delete back to the mistake, then type it again. If you notice the mistake after entering the line, just retype the line and your second version will replace the original.

Although we’ve entered all that information, so far we haven’t got the result. To get the result we have to use the command RUN (the R key). The program is being held in the computer’s memory, and you can display it by using the LIST command (the K key). Using list by itself will display the complete program. You can display a program from a specific line by using LIST followed by the line number (e.g. LIST 200).

You’ll notice we used line numbers that increased by 100 each time, but that isn’t a specific requirement, but it makes it easy to adjust the program.

If we now enter the lines
330 LET T$ = “AREA OF SQUARE IS ”
350 LET T$ = T$ + STR$ SQUARE + “ SQUARE FEET”
we see that our two lines have been inserted into the program in the correct place. However, we now need to change the PRINT statement, and we can do that by retyping line 400 in the new form:
400 PRINT T$

We will now see that the old line 400 has been replaced. You can delete a program line by typing its line number and immediately pressing NEWLINE.

Using the RUN statement again will show the new result displayed. If you get an error message, use the LIST command and check the program, retyping any line with an error in it; an error message like 2/130 means there is an error in line 130.

However, our program is not very useful yet, because it always will use the same values for length and width, unless you change the program itself.

Type the following lines (INPUT is obtained from the I key):
100 INPUT LENGTH
200 INPUT WIDTH

You’ll see that the two new lines have replaced the original lines with those numbers. If you RUN the program you’ll see a blank screen with an L cursor (which means it’s in Input mode) in the bottom left-hand corner. Type in a number (say 20) and press NEWLINE. There’s another L cursor waiting for input, so type another number (say 33) and press NEWLINE. Now you’ll see the result (which will be AREA OF SQUARE IS 660 SQUARE FEET, if you used the suggested numbers.)

Let’s add some prompts, so we know what we are supposed to be doing, and make the program loop, so that we can calculate more areas. Add the following lines (CLS is the V key, GOTO is G):
50 PRINT “ENTER THE LENGTH”
120 CLS
130 PRINT “ENTER THE LENGTH ”; LENGTH
150 PRINT “WIDTH ”;
220 PRINT WIDTH
500 PRINT
600 GOTO 50

The GOTO statement redirects the program flow to a different part of the program, and in our case it sends the program back to line 50. If you want to get fancy, the line-number that the GOTO statement sends the program to doesn’t have to be entered specifically, but could be a variable or a calculation, so our line could have been something like:
600 GOTO 10 * VARIABLENAME (but don’t use that line in your program now).

Please note that programmers these days seem to have a pathological dislike for the GOTO statement ;-)

We should see that the full program is now:
50 PRINT “ENTER THE LENGTH”
100 INPUT LENGTH
120 CLS
130 PRINT “ENTER THE LENGTH ”; LENGTH
150 PRINT “WIDTH ”;
200 INPUT WIDTH
220 PRINT WIDTH
300 LET SQUARE = LENGTH * WIDTH
330 LET T$ = “AREA OF SQUARE IS ”
350 LET T$ = T$ + STR$ SQUARE + “ SQUARE FEET”
400 PRINT T$
500 PRINT
600 GOTO 50

What happens when the program runs is that it prompts you to enter the length, once you have done that it clears the screen (using the CLS command – the V key), and we clear the screen because we may be doing other calculations later and we don’t want to fill the screen up. Then is displays what you entered for the length and prompts you for the width (we add a semi-colon at the end of the statement so that when we print the width it gets added on the same line). After entering the width, the result is calculated and displayed, and program then adds a blank line and goes back to line 5 (using the GOTO statement) and prompts you for another Length.

If you don’t want to carry out another calculation, you can enter the STOP command (Shift+A). If you do that, and then decide you actually have more areas to calculate, you could either RUN the program again, or enter the CONT command (the C key, meaning CONTinue). You could also start again using a statement like GOTO 400, which will jump to line 400 and reprint the previous result, before prompting for a length again. RUN 400 will also jump to line 400, but RUN clears the variables, so an error message will be generated. RUN 500 would work.

Now let’s save our program, so we have it available for the next lesson. Type the following line (SAVE is the S key):
SAVE “AREAS”

Then you can press NEWLINE (on a real ZX81 you’d have started your tape recorder first, because the computer used to record programs and data to an audio tape cassette, but the emulator software uses a disk to record this information normally.)

If you want to try loading the program, enter the NEW command (from the A key) to clear the program from memory, then enter the command
LOAD “AREAS”

The ZX81 keyboard

The ZX80 with its original RAM-pack attached and a larger RAM-pack alongside

Copyright www.LaraAcademy.com