statement/FOR statement/FOR
NAME
FOR -- Defines a program loop
ABBREVIATION
f <shift> O
SYNOPSIS
FOR <loop_var>=<start_val> TO <end_val> [STEP <increment>]
FUNCTION
This statement works with the NEXT statement to set up a section of the
program that repeats for a set number of times. You may just want your
computer to count up to a large number so the program pauses for a few
seconds, in case you need something counted, or something must be done a
certain number of times (such as printing).
The loop variable (<loop_var>) is the variable that is added to or
subtracted from during the FOR-NEXT loop. The start value (<start_val>)
and the end value (<end_val>) are the beginning and ending counts for
the loop variable.
The logic of the FOR statement is as follows. First, the loop variable
(<loop_var>) is set to the start value (<start_val>). When the program
reaches a line with the command NEXT, it adds the STEP increment
(<increment>) to the value of the loop variable and checks to see if it
is higher than the end of loop value. If it is not higher, the next line
executed is the statement immediately following the FOR statement. If
the loop variable is larger than the end of loop number, then the next
statement executed is the one following the NEXT statement.
The end loop value may be followed by the word STEP and another number
or variable. This allows you to count backwards, by fractions, or any
way necessary.
INPUTS
<loop_var> - variable which holds the loop counter value
<start_val> - start value for loop variable (<loop_var>)
<end_val> - end value for loop variable (<loop_var>)
<increment> - value to be added to or subtracted from loop variable
RESULT
Performs the statements between the FOR statement and the NEXT statement
until the loop variable reaches the end value.
EXAMPLES
10 FOR L=1 TO 20
20 PRINT L
30 NEXT L
40 PRINT "BLACKJACK! L="L
Prints the numbers from one to twenty ob the screen, followed by the
message BLACKJACK! L=21.
10 FOR L=1 TO 100
20 FOR A=5 TO 11 STEP 2
30 NEXT A
40 NEXT L
FOR-NEXT loop with loop variable A is nested inside the larger one.
NOTES
STEP increment default value is 1.
A STEP value can be positive or negative.
You can set up loops inside one another. This is known as nesting loops.
You must be careful to nest loops so that the last loop to start is the
first one to end.
BUGS
None
SEE ALSO
DO
NEXT