The first time through the loop, therefore, SX will be replaced with the sum of zero,
the initial value stored in the variable SX, and one (the first period number).
Similarly, XX will be replaced with the sum of zero and one times one. The variable
XX now has the value one. The variable SY
is
replaced withthe sum of zero and the
first
period'~
sales (103 in the example above), and the variable XY
is
replaced with
the sum of zero and one times the first period's sales (again,
103
in the example).
Table
7-1
below illustrates how the four variables SX,
XX,
SY
and XY sum
up
the
appropriate quantities on each repetition of the loop:
Loop cycle
X
Y
X*X
X*Y
SX
XX
SY
XY
0
0 0
0 0 0 0
0 0
1
I
103
1
103
1 1
103
103
2 2
110 4 220
3
5
213
323
3
3 109 9
327
6
14
322 650
4 4 120
16
480
10
30 442 1130
5
5
119
25
595
15
55
561
1725
6 6
133
36
798
21
91
694 2523
Table 7-1. Sales Trend Line Calculations
Line
80
The NEXT X statement determines the end of the loop of statements which
are
to
be repeated with successive values for the index variable
X.
So long
as
the
value of X is less than the upper limit specified in the matching FOR X statement, the
loop will start over again with the next value for
X.
When X reaches its upper limit
(N
in
this case), execution transfers
to
the statement
immediately following the NEXT X statement (line
90 in this case).
There must be a NEXT statement to match every FOR statement
in
a program; without
a NEXT
:statement, there would be no way to determine the end
of
the FOR loop. and
therefore no way to know when
to
repeat the loop.
Lines 90 . 100 The assignment statements compute the slope, B, and intercept, A,
which define the straight line which best fits the sales data. The use of parentheses
was
necessary in the expression
to
properly compute the ratios:
B=
N
*XY
SX * SY
N<XX
SX * SX
and
A=
SY
B * SX
N
Using the sample data entered above, A and B are computed
as:
and
B
6 * 2523
21
* 694
21
*
21
91
5.37143
)'