IBM 5110 Basic Introduction page 66

Table of Contents

Advertisement

62
If you're using FOR and NEXT statements for the loop, you add the
word STEP and the size of the step to the FOR statement. For
example:
0010 FOR X=1 TO 25 STEP 2
gives you odd values of X from 1 to 25 (1, 3, 5, 7 ... ).
0030 FOR D=10 TO 100 STEP 10
gives you 10, 20, 30, up to 100.
For even values of D from 1 to 20, you would write:
0020 FOR D=2 TO 20 STEP
2
Notice that D is set to 2 because the first even number you want is 2.
If you omit the word STEP and the value from the FOR statement, you
automatically get steps of 1. You can also include fractional steps, for
example:
0030 FOR 1=1 TO 3 STEP .1
Loops Within Loops
Here's a problem in which two values change: Find the annual amount
of interest (A) at the interest rates
(I)
of 5 %, 6 %, 7 %, 8 %, 9 %, and
10% on principals (P) ranging from $100 to $1000 in steps of $100.
This problem can be solved by a program that uses two loops-one for
changing the interest and one for changing the principal. Do the
interest loop first:
0030 FOR 1=5 TO 10
0040 A=( 1/1
OO}*P
0050 PRINT P, I, A
0060 NEXT I
}
This computes
and displays A,
P, and
I.
This creates a
loop for I to vary
from 5% to 10%.
Now all that's left is to define P, because the program doesn't know
where to find the values for P. The P loop has no computations of its
own; it only defines the values for P:
0020 FOR P=100 TO 1000 STEP 100
0070 NEXT P
/1(- "",
,,-, . . . .1
. 1 " - ) \
'\.j"
o
62
If you're using FOR and NEXT statements for the loop, you add the
word STEP and the size of the step to the FOR statement. For
example:
0010 FOR X=1 TO 25 STEP 2
gives you odd values of X from 1 to 25 (1, 3, 5, 7 ... ).
0030 FOR D=10 TO 100 STEP 10
gives you 10, 20, 30, up to 100.
For even values of D from 1 to 20, you would write:
0020 FOR D=2 TO 20 STEP
2
Notice that D is set to 2 because the first even number you want is 2.
If you omit the word STEP and the value from the FOR statement, you
automatically get steps of 1. You can also include fractional steps, for
example:
0030 FOR 1=1 TO 3 STEP .1
Loops Within Loops
Here's a problem in which two values change: Find the annual amount
of interest (A) at the interest rates
(I)
of 5 %, 6 %, 7 %, 8 %, 9 %, and
10% on principals (P) ranging from $100 to $1000 in steps of $100.
This problem can be solved by a program that uses two loops-one for
changing the interest and one for changing the principal. Do the
interest loop first:
0030 FOR 1=5 TO 10
0040 A=( 1/1
OO}*P
0050 PRINT P, I, A
0060 NEXT I
}
This computes
and displays A,
P, and
I.
This creates a
loop for I to vary
from 5% to 10%.
Now all that's left is to define P, because the program doesn't know
where to find the values for P. The P loop has no computations of its
own; it only defines the values for P:
0020 FOR P=100 TO 1000 STEP 100
0070 NEXT P
/1(- "",
,,-, . . . .1
. 1 " - ) \
'\.j"
o

Advertisement

Table of Contents
loading

Table of Contents