``

prev Menu next

Introduction to BASIC FOR～NEXT
FOR～NEXT (1)
Example. A program that calculates SQR(x) for x=0，0.1，0.2，... ，1.

    10 FOR x=0 TO 1 STEP 0.1
    20     PRINT x,SQR(x)
    30 NEXT x
    40 END

When the FOR statement in line 10 is executed for the first time, it assigns 0 to x, and the program proceeds to line 20.
When the NEXT statement in line 30 is executed, it adds 0.1 to x, and lets the control goes back to the line following the FOR statement
This repetition continues until x exceeds 1.
The number written just after STEP is called an increment.
[Note]
Extra spaces have no meanings, though they are used for readability.
FOR～NEXT (2)
If STEP is omitted, the increment is assumed to be 1.
Example. A program which calculates x2 for x=1,2,3, ... ,10.

    10 FOR x=1 TO 10
    20    PRINT x,x^2
    30 NEXT x
    40 END

Refer to FOR～NEXT Detail