•
•
There are
five
programs below which
all
do the same thing: they cause the four 'houses'
to
be bccupled' and they PRINT
to
show that the 'occupation' has really worked. The
final method uses only four lines but the other four lead up
to
It
in
a
way
which moves
all
the time from known ideas
to
new ones or new uses of old
ones.
The movement
is
also towards greater economy.
If
you
understand the
first
two or three methods perfectly
well
you may prefer
to
move
straight onto methods 4 and
5.
But
If
you are
in
any doubt. methods
1,
2 and 3
will
help
to
clarify things.
100
DIM
high
st$(4,3)
110
LET
high-st$(1)
"VAL"
120
LET
high-st$(2)
=
"HAL"
130
LET
high-st$(3)
"MEL"
140
LET
high-st$(4)
=
"DEL"
150
PRINT'
high
st$(1l
I
high
st$(2)
I
160
PRINT'
high=st$(3)
I
high=st$(4)
I
100
DIM
high
st$(4,3)
110
READ
high
st$(1l,high
st$(2),high st$(3),high
st$(4)
120
PRINT I
high
st$(1l
I
high
st(2)
,-
-
130
PRINT'
high-st$(3)
I
high
st(4)
I
140
DATA
"VAL",
.....
HAlll,
"MEL",
'j"j'""OEL"
Program 1
Program 2
,
Arrays
and
For
Loops
•
This shows how
to
economise
on
variable names but the constant repeating
of
high_st$
is
both tedious and the cause of the cluttered appearance of the programs.
We
can,
again, use a known technique - the REPeat loop -
to
Improve things further.
We
set
up
a counter, number, which increases by one as the REPeat loop proceeds.
100
RESTORE
190
Program 3
110
DIM
high
st$(4,3)
120
LET
number
=0
130
REPeat
houses
140
LET
number
=
number
+ 1
150
READ
high
st$(number)
160
IF
num
=
4THEN
EXIT
houses
170
END
REPeat
houses
180
PRINT
hi
gh
St<1l1
hi
gh
st
(2)
I
hi
gh
st
(3)
I
hi
gh
st(4)
190
DATA
"VAL"-;-
"HAL",
"MEl",
"DEL"
This special type of loop,
In
which something has
to
be done a certain number of times,
is
well
known. A special structure, called a FOR loop, has been invented
for
it.
In
such
a loop the count from 1
to
4
is
handled automatically.
So
is
the
eXit
when
all
four items
have been handled.
1
00
RESTORE
160
Program 4
110
DIM
high
st$(4,3)
120
FOR
number
=1
TO
4
130
READ
high
st$(number)
140
PRINT I
high
st$(number)
I
150
END
FOR
number-
160
DATA
IIVAL","HAL","MEl'l,"DEL"
The output from
all
four programs
is
the same:
VAL
HAL
MEL
DEL
Which proves that the data
is
properly stored internally
in
the four array variables:
12/84
G
1
a
2
a
3
a
4
29