Making loops
A loop makes a computer
do something, such as
display a message, over
and over again. To make a
loop in a program, you use
the commands FOR, TO
and NEXT.
Uglymug loop
10 CLS
20 FOR K=1 TO 5
30 PRINT "HELLO UGLYMUG"
40 NEXT K
The loop in this program
begins with the FOR, TO
command in line 20. It tells
the computer how many
times to repeat the PRINT
command. The command,
NEXT, in line 40 tells the
computer to loop back and
carry out the PRINT
command again.
How a loop works
A loop is rather like a race where
the runner has to go round the
track several times. Each
time the computer
carries out the
instruction in a loop, it
is like running one lap
of the race.
NEW
10 CLS
20 FOR J = 1 TO 6
30 PRINT "HELLO"
40 NEXT J
RUN
In this loop program, the
computer uses the variable J
to keep count of the laps. J is called the "loop variable".
At the beginning, the number
in J is 1. Each time the computer goes round the loop, the number in J goes up
by 1 until it reaches 6. Then
the loop is finished.
Looking inside a loop
This program lets you look inside a loop, so
you can see the computer keeping count of
the laps.
NEW
10 CLS
20 FOR L=1 TO 20
30 PRINT "LAP ";L
40 NEXT L
RUN
Loop puzzle!
Can you complete this program, so the
computer displays your name ten times.
You need to fill in the numbers in line 20,
and put your name in quotes after the
PRINT command in line 30.
NEW
10 CLS
20 FOR J= TO
30 PRINT
40 NEXT J
RUN
Loopy programs
Here are some more ways of using loops in programs.
Counting program
Loops are very useful for
making a computer count.
Try this program, which
makes the computer count up to any number you like.
When you run the program,
you type in the number you
want the computer to count to.
NEW
10 CLS
20 PRINT "WHAT NUMBER SHALL"
30 PRINT "I COUNT TO?"
40 INPUT N
50 FOR K=1 TO N
60 PRINT K
70 NEXT K
RUN
Different ways of counting
You can change the way the
computer counts. For
instance, you can make it
count in jumps. To do this,
you use the command
STEP followed by a number.
The program below makes
the computer count in steps
of 5. Try running it.
NEW
10 CLS
20 FOR L=0 TO 30 STEP 5
30 PRINT L
40 NEXT L
RUN
Countdown
STEP with a minus number
makes the computer count
backwards. Try this
countdown program to see
how it works.
NEW
10 CLS
20 PRINT "COUNTDOWN"
30 FOR J=10 TO 1 STEP -1
40 PRINT J
50 NEXT J
60 PRINT "BLAST OFF!"
RUN
Making the computer wait
A loop with no instructions inside it makes
the computer pause for a moment. It is called
a delay loop. In the program below, the delay
loop in lines 30 and 40 makes the computer
pause before it displays the second message.
You can alter the length of the pause by
changing the second number in the FOR, TO
command. Have a go at shortening or
lengthening the pause in the
program.
10 CLS
20 PRINT "HELLO EVERYBODY"
30 FOR K=1 TO 1000
40 NEXT K
50 PRINT "HELLO AGAIN"
Using the loop variable
This program makes the
computer display the nine-
times table. The loop variable,
K, is used both for counting
the loops, and for doing the
sum inside the loop.
NEW
10 CLS
20 FOR K=1 TO 12
30 LET A = K*9
40 PRINT K;" x 9 =";A
50 NEXT K
RUN
In this next program the computer
uses the loop variable, J, as the
number for the PRINT TAB
command. This means that each time the computer repeats
the loop it leaves one more space before the word HELLO.
NEW
10 CLS
20 FOR J=1 TO 10
30 PRINT TAB(J); "HELLO"
40 NEXT J
RUN