Lesson 10

PAUSE, RND, RANDOMIZE, STOP, CONTINUE

This is the final lesson “BASIC for Beginners”. After you have completed this lesson we suggest that you have some fun with coding, and at the same time get some coding practice, by writing several programs of your own.

If you get plenty of practice in using the 50 BASIC codewords you’ve learned in this beginners course, you will find it easy to move on to the Animation and Game Design lessons. And later on, when you are confidently coding your own games, you can learn even more about BASIC from “BASIC for the More Advanced Pupils”.

PAUSE This codeword tells the computer to wait a certain amount of time before it continues running the program.

RND This codeword is used when you want the computer to “think” of a number – any number – so you can sue that number in your program. This type of number is called a “RaNDom” number, which is where the codeword RND comes from.

RANDOMIZE This codeword can be used, together with RND, to produce a different sequence of random numbers each time the program is run. It can also be used to create the same sequence of random numbers every time.

STOP This codeword stops a program from running. It can be a very useful codeword if you need to find a bug in your program.

CONTINUE If you have stopped the program when it was running, the CONTINUE codeword tells the computer to restart it.

The PAUSE codeword

The PAUSE codeword is a statement/command.

You can use the PAUSE codeword to suspend a program for as long as you like.

How to use the PAUSE codeword

PAUSE is very easy to use. Here is an example.

130 PAUSE 50

The number following PAUSE may be anything from 0 to 65535. If the number is 50, and if you are in the UK, then program line 130 will suspend the running of the program for 1 seconds. So to create a delay of almost any amount of time you wish, simply multiply 50 by the number of seconds you want the computer to pause.

If you want to tell the computer to suspend the program until you tell it to start again, your program line should say PAUSE 0  and the program will be suspended until you press a key (any key).

But any pause may be cut short by pressing any key. So even if you put a very long pause in your program, once the computer pauses the running of the program you can restart the program yourself just by pressing any key.

The RND codeword

The RND codeword is a function.

The RND codeword is used to generate a random number which is greater than or equal to 0, but less than 1. A random number means a number chosen by chance. And because it is chosen by chance we never know what number it will be, until after it is generated by using the RND codeword.

How to use the RND codeword

Random numbers are used in all sorts of different ways in computer coding. As a simple example, think of tossing a coin. The coin come down heads or tails, but we don’t know in advance which it will be. Heads and tails are equally likely.

So let’s see how we could write part of a program to do something very like tossing a coin.

50 LET coin = RND

51 IF coin < 0.5 THEN PRINT "Heads"

52 IF coin > 0.5 THEN PRINT "Tails"

If you think about what the computer does with thee three lines of program you will realise that what it is doing has a very similar effect to tossing a coin.

In line 50 the computer creates a random number between 0 and 1 and assigns that number to be the value of the variable called coin. So coin now has a value between 0 and 1 but we don’t know what that value is. The value is just as likely to be less than 0.5 as it is to be greater than or equal to 0.5. And tossing a coin is just as likely to result in heads as it is to be tails.

In lines 51 and 52 the computer displays the word Heads or the word Tails, depending on whether the value of coin is less than 0.5 or whether it is greater than or equal to 0.5

How to create random whole numbers

You might want to tell the computer to generate a random whole number. You can do this by using the RND codeword together with one we haven’t yet learned about – the INT codeword. We will explain how this works in Lesson 7, but just in case you want to try out using whole random numbers in one of your programs we are jumping ahead of ourselves a little by showing you this:

60 INT (RND*w + 0.5)

61 REM this generates a random whole number between 0 and w.

The RANDOMIZE codeword

The RANDOMIZE codeword is a command/statement.

RANDOMIZE can be used in conjunction with RND to produce sequences of numbers that are random. It is also possible to use RANDOMIZE and RND to produce the same sequences of numbers over and over again.

How to use the RANDOMIZE codeword

You can tell the computer to produce a sequence of numbers that are random like this:

10 RANDOMIZE

or

10 RANDOMIZE 0

and then every time RND produces a number it will be a random number.

But if you want to tell the computer to produce the same sequence of numbers every time, then you should use the RANDOMIZE codeword like this:

10 RANDOMIZE number

where number can be any number between 1 and 65535.

The STOP codeword

The STOP codeword is a statement/command.

As you would expect from its name, the STOP codeword stops a program at the point where you put the codeword. You might want to use STOP to end the main section of a program so you can put some subroutines after it, and make sure that the computer only obeys the instructions in a subroutine if it gets there by jumping to the subroutine as a result of obeying a GO SUB command.

STOP is sometimes useful when you are debugging a program. It allows you to run the program up to a certain point, and then stop it and take a look at the values of some of your variables, for example, to see if they are what you expect them to be.

How to use the STOP codeword

STOP is very easy to use. You simply put it in a program line by itself.

When the program stops, it displays an error report (this is report 9), the program line number and the statement in that line where it stopped.

Entering CONTINUE subsequently causes the program to start up again. (This is the next codeword explained in this lesson.)

The CONTINUE codeword

The CONTINUE codeword is a command.

If a program stops, CONTINUE can be used to restart the program from the point at which it stopped. If the program has stopped because of a “bug”  then it must be corrected before CONTINUE will allow the program to start up again.

How to use the CONTINUE codeword

CONTINUE is used as a direct codeword when a program has stopped. It requires no parameters. After CONTINUE a program then normally resumes at the same statement at which it stopped. If the cause was an error, then a codeword can be entered to rectify the error and CONTINUE will allow the program to continue from that statement. If the program stopped at a STOP statement giving report 9 (“STOP statement”) or if it halted because the BREAK key was pressed giving report L (“BREAK into program”), then CONTINUE causes the program to resume from the next statement. A rectifying codeword can be entered first if necessary.

If CONTINUE is used to resume a direct codeword, then it will go into a loop if the codeword stopped at the first statement in the codeword. The display disappears, but control can be regained by pressing BREAK. CONTINUE gives report 0 (“Ok”) if the codeword stopped at the second statement and report N (“Statement lost”) at the third or subsequent statements.

CONTINUE is used like RUN  on its own with no line number. It resumes the running of a program at the next statement (unless you STOPped or pressed BREAK during an INPUT, in which case CONTINUE will repeat the INPUT command.

Example Program

This program is a word guessing game.Lines 1060 to 1150 select a random word from our list of words. Lines 1180 to 1310 display some information about the word.The lines from 1330 to 1520 ask for a letter and check if it’s in our word. Lines 1530 to 1760 check if the game has ended and if so display who won. Lines 1770 to 1830 asks if you want to play again.

1000 REM Guess a Word
1010 RANDOMIZE
1020 REM Tell the computer to think of random numbers
1030 INK 0: PAPER 7: FLASH 0: CLS
1040 REM Select black ink and white paper
1050 REM Turn off flashing and clear the screen
1060 LET N=10: REM The number of words we know
1070 LET x=INT (RND*N)+1
1080 REM Get a Random number between 1 and N
1100 REM Restore the data to the start
1110 READ w$
1120 REM Read a word
1130 LET x=x-1
1140 REM decrement the counter
1150 IF x>0 THEN GO TO 1110
1160 REM and if it is still greater then 0 GOTO line 1110
1170 REM This selects the xth word in our list
1180 LET b=LEN w$: LET v$=" ": LET c$=""
1190 REM Add spaces to v$ for each letter in our word
1200 REM v$ is the word guessed so far
1210 REM Make c$ empty because we keep the letters already guessed in it
1220 FOR n=2 TO b: LET v$=v$+" "
1230 NEXT n:
1240 LET c=0: LET d=0
1250 REM c is the number of guesses and
1260 REM d is the number of mistakes
1270 PRINT AT 0,0;"My word is ";LEN (w$);" letters"
1280 REM Display a message to say how long our word is
1290 FOR n=0 TO b-1
1300 PRINT AT 20,n;"-"
1310 NEXT n
1320 REM write "-" symbols instead of letters
1330 INPUT "Guess a letter: ";g$
1340 REM Ask the player for a letter
1350 IF g$="" THEN GO TO 1330
1360 REM If they didn't enter anything ask again
1370 LET g$=g$(1): REM 1st letter only
1380 GO SUB 1880
1390 REM Call our subroutine to check if we have already guessed that letter
1400 IF y=1 THEN GO TO 1330
1410 REM If y=1 we have already guessed that letter so ask for another
1420 LET c$=c$+g$
1430 REM Add the letter to our list of guessed letters
1440 PRINT AT 2,c;"Letters guessed:";c$
1450 REM And display a message to say what letters we have guessed
1460 LET c=c=1: LET u$=v$
1470 REM Keep a copy of what we have guessed
1480 FOR n=1 TO b
1490 IF w$(n)=g$ THEN LET v$(n)=g$
1500 REM Update the guessed word by checking for correct
1510 REM letters and adding them to our guessed string
1520 NEXT n
1530 PRINT AT 19,0;v$
1540 REM Display our guessed word so far
1550 IF v$=w$ THEN GO TO 1750
1560 REM If the word has been guessed GOTO line 1750
1570 IF v$<>u$ THEN GO TO 1330
1580 REM If the guessed word so far is now different from the copy
1590 REM the letter guessed was correct so keep asking for letters
1600 LET d=d+1
1610 REM add 1 to count of mistakes
1620 IF d=9 THEN GO TO 1710
1630 REM If the player has made 9 mistakes they have lost
1640 IF (d=1) THEN PRINT AT 15,0;d;" wrong guess"
1650 IF (d<>1) THEN PRINT AT 15,0;d;" wrong guesses"
1660 REM Display the number of mistakes
1670 PRINT "You have ";9-d;" left"
1680 REM Display how many guesses left
1690 GO TO 1330
1700 REM and keep playing
1710 LET m$="YOU LOSE": GO SUB 1960
1720 REM Use our subroutine to tell the player they have lost
1730 GO TO 1770
1740 REM And jump to line 410
1750 LET m$="YOU WIN": GO SUB 1960
1760 REM Use our subrotuine to tell the player they have won
1770 INPUT "Would you like to play again? ";a$
1780 REM Ask if they want to play again
1790 IF (a$="y") OR (a$="Y") THEN RESTORE : GO TO 1030
1800 REM If they do reset the data and go to the start
1810 CLS : PRINT AT 19,0;"Thanks for playing"
1820 REM If not display a message
1830 STOP
1840 REM and STOP
1850 DATA "dog","cat","house","aeroplane","kangaroo"
1860 DATA "football","cricket","rugby","flower","tree"
1870 REM This is our list of words
1880 REM This is our subroutine to check if have already guessed the letter
1890 FOR n=1 TO LEN (c$)
1900 IF c$(n)=g$ THEN LET y=1: RETURN
1910 REM If the letter is in our list of already used letters set y to 1
1920 NEXT n
1930 LET y=0
1940 REM If it's not in our list set y to 0
1950 RETURN
1960 REM This is our subroutine to display a message at the end of a game
1970 REM The message is in the string m$
1980 FOR n=0 TO 31: PRINT AT 15,n;" ": PRINT AT 16,n;" ": NEXT n
1990 REM First we remove the message with the number of guesses
2000 INK 2: FLASH 1: BRIGHT 1: PRINT AT 14,0;m$
2010 REM Then we display the message in Red flashing
2020 INK 0: FLASH 0: BRIGHT 0
2030 RETURN