Page 58 - 1
P. 58
INPUT A
SUM = SUM + A
NEXT S
PRINT SUM
END
When we run this program then the control variable is assigned a value and a final value. It
will take all the numbers between the initial and final values. Then the statement is
executed and the NEXT loop sends the control back to the FOR statement. Then the new
value is taken and the action is performed. At last stage the answer is displayed on the
screen.
For...Next With Step
FOR...NEXT WITH STEP statement is used to increase or decrease the value of the control
variable by our own choice. STEP value is optional and can either be positive or negative.
Syntax
FOR <control variable>=<initial value> TO <final value> STEP <n>
Example
REM Program to print first four odd numbers.
CLS
FOR A = 1 to 8 STEP 2
PRINT A
NEXT A
END
In this program the first four odd numbers 1, 3, 5, 7 will be displayed on the screen.
Nested-For...Next
This statement is used as nested loop for FOR...NEXT statement. The FOR...NEXT loop which
is inside is known as inner loop and which is outside is known as outer loop.
Syntax
FOR C=1 TO 10
FOR D=1 TO 10
NEXT D
NEXT C
END
Remember that you can only have maximum of 9 loops within a loop. There should be an
58