Google
 

Control Statements, Loops, and Subroutines

In the previous lesson we mentioned that the NO$ZX81 emulator’s default setup lets you enter a number of special characters from their regular position on a standard PC keyboard (rather than having to remember where they are found on an actual ZX81 keyboard). But sometimes we might want, or need, to resort to the standard ZX81 keyboard layout, so how do we change the keyboard type? On starting up NO$ZX81 click on the Options menu, then Emulation Setup and you’ll probably see that Key Translation is set to Automatic. From the drop list you’ll see that Original ZX Keyboard is an option. For now we can leave it at Automatic, but we’ll switch keyboard options when needed from this window, which can be accessed by using the F11 key.

First we need to go to the Run menu and select Run, then load the program that we created during the last lesson by entering the command
LOAD “AREAS”

Then add the following lines, which use some new commands, as follows:
REM – the E key
GOSUB – the H key
STOP – Shift+A
IF – the U key
<> - Shift+T
THEN – Shift+3
RAND – the T key
INT – Shift+NEWLINE, then the R key
RND – Shift+NEWLINE, then the T key
RETURN – the Y key
TO – Shift+4, but you might first need to press F11, set Key Translation to Original ZX Keyboard, then do Shift+4, then do F11 again and set Key Translation back to Automatic.
LEN – Shift+NEWLINE, then the K key
>= - Shift+Y
AND – Shift+2
<= - Shift+R
NEXT – the N key
VAL – Shift+NEWLINE, then the J key

20 REM “THIS PROGRAM CALCULATES AREAS”
30 PRINT “PLEASE USE NUMERALS FOR THE ENTRIES”
40 PRINT “OR I WILL GUESS ONE MYSELF”
45 PRINT
105 LET A$ = L$
110 GOSUB 2000
115 LET LENGTH = SUBVAL
205 LET A$ = W$
210 GOSUB 2000
215 LET WIDTH = SUBVAL
2000 REM “THIS SUBROUTINE CHECKS THE ENTRIES”
2005 IF A$=”END” THEN STOP
2010 IF A$ <> “” THEN GOTO 2050
2020 RAND 0
2030 LET SUBVAL = INT (RND * 100)
2040 RETURN
2050 LET C$ = “”
2060 FOR L = 1 TO LEN A$
2070 LET B$ = A$(L)
2080 IF B$ >= CHR$ 28 AND B$ <= CHR$ 37 THEN LET C$ = C$ + B$
2090 NEXT L
2100 IF C$ = “” THEN LET SUBVAL = LEN A$
2110 IF C$ <> “” THEN LET SUBVAL = VAL C$
2120 RETURN

REM, used in lines 20 & 2000, is used to let the computer know that the text that follows is a comment or REMark

GOSUB/RETURN is used in lines 105-115, 205-215, 2000-2120, and sets up a subroutine that can be called from various places in the program (so you can easily reuse the code)

IF, used in lines 2010, 2080, 2100, and 2110, lets you execute particular code based on whether a specific function evaluates as true or not

FOR/NEXT, as used in lines 2060 – 2090, lets you loop a number of times through the same section of code

RAND is used with the function RND; RND uses a fixed sequence of 65536 numbers, and RAND can be used to set where RND will use a start point in the sequence by assigning a number between 1 and 65535, or by using RAND 0 (or just RAND) it will use the time the system has been on, and therefore should be really random (although the way we use it, it gets called just before RND each time, so the random sequence never really gets used).

The full program now looks like:
20 REM “THIS PROGRAM CALCULATES AREAS”
30 PRINT “PLEASE USE NUMERALS FOR THE ENTRIES”
40 PRINT “OR I WILL GUESS ONE MYSELF”
45 PRINT
50 PRINT “ENTER THE LENGTH”
100 INPUT L$
105 LET A$ = L$
110 GOSUB 2000
115 LET LENGTH = SUBVAL
120 CLS
130 PRINT “ENTER THE LENGTH ”; LENGTH
150 PRINT “WIDTH ”;
200 INPUT W$
205 LET A$ = W$
210 GOSUB 2000
215 LET WIDTH = SUBVAL
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
2000 REM “THIS SUBROUTINE CHECKS THE ENTRIES”
2005 IF A$=”END” THEN STOP
2010 IF A$ <> “” THEN GOTO 2050
2020 RAND 0
2030 LET SUBVAL = INT (RND * 100)
2040 RETURN
2050 LET C$ = “”
2060 FOR L = 1 TO LEN A$
2070 LET B$ = A$(L)
2080 IF B$ >= CHR$ 28 AND B$ <= CHR$ 37 THEN LET C$ = C$ + B$
2090 NEXT L
2100 IF C$ = “” THEN LET SUBVAL = LEN A$
2110 IF C$ <> “” THEN LET SUBVAL = VAL C$
2120 RETURN

The ZX81 keyboard

The upgrade kit (new keyboard and chip) to bring a ZX80 up to near ZX81 standard

Copyright www.LaraAcademy.com