The variable TOTAL
is changed inside the loop body.
Doesn't this violate one of the rules for the FOR
loop?
No. Only the variable that is keeping the count (here COUNT
)
should not be changed.
The starting value of a FOR
loop does not have to be positive.
Here is the addition program again, with different start and
finish:
' Add up the integers from START to FINISH ' LET TOTAL = 0 ' TOTAL will hold the sum LET START = -2 LET FINISH = 3 ' FOR COUNT = START TO FINISH LET TOTAL = TOTAL + COUNT PRINT COUNT, "has just been added to TOTAL which is now", TOTAL NEXT COUNT ' PRINT "The total is:", TOTAL END
For now, ignore what happens to TOTAL
and concentrate about
what happens to COUNT
.
The FOR
statement says to use values
for COUNT
of -2, -1, 0, 1, 2 and 3.
Remember to include the zero, since zero is as good a number as any other.
How many times did the loop body execute?