Radio Shack TRS-80 Model 100 Basic Manual page 103

Basic language lab
Hide thumbs Also See for TRS-80 Model 100:
Table of Contents

Advertisement

Experiment #4 Four Seasons
To compute the average ratio to trend for the second quarter, you could simply change
the start value of X to 2 in line 140:
140 FOR X
=
2 TO N STEP 4
A start value of 3 would compute the third quarter ratio and a start value of 4 would
compute the fourth quarter ratio. The message "QUARTER I RATIO IS" would still
appear for every quarter since nothing has been done to line 170. Rather than
manually change line 140 for each quarter, however, you could modify the program to
do this for you. Enter the following changes to the program:
130 FOR Q
:::
1 TO 4
135 R
=
0
140 FOR X
=
Q TO N STEP 4
170 PRINT "QUARTERljQj" RATIO IS"jR/6
180 NEXT Q
Run this program to confirm that it now prints the average ratio to trend for all four
quarters:
1'+8.22463768116
QUARTER 1
RATIO
QUARTER 2
RATIO
QUARTER 3
RATIO
QUARTER 4
RATIO
Ok
X IS
+
8.4086956521739
IS .9230187061999
IS 1.0761822655641
IS .86424468709917
IS 1.1358281261464
Lines 138 and 198 define a program loop which repeats for each of the four quarters.
Notice that this loop contains within it another loop from line 140 to line 160. This is
an example of a programming concept known as "nested loops."
It
simply means
that there is a loop of statements within a loop of statements.
The inner FOR / NEXT loop must be completely contained within the outside loop, no
overlap is allowed. In this experiment, the inner loop, lines 140 to 160, compute the
average ratio to trend for a specified quarter Q, and the outer loop, lines 130 to 190,
which repeats for each quarter with the index variable Q
=
1, 2, 3 and 4.
Line 135 resets the ratio sum variable R to zero before starting the calculation for
each quarter. This is now necessary to avoid starting with the sum remaining from the
previous quarter.
Line 178 was changed to print the quarter Q along with the average ratio to trend R/6.
97

Advertisement

Table of Contents
loading

Table of Contents