Qbasic Tutorial for beginners and children (Under Construction)


The FOR NEXT Statement

The FOR NEXT statement is used to repeat a block of instructions a specified number of times. The syntax is as follows:

FOR counter = start TO end [STEP increment]

    [block of instructions]

NEXT

 

In the FOR NEXT statement a block of instructions is executed a number of times. The value of the variable counter is incremented by 1 until it equals to the value of end.

 

The variable counter, by default, increases by 1. You can change the rate at which the variable counter increments after each revolution by using the STEP parameter: See example 2 for an illustration.

Examples:

Example 1:This program print integers from 1 to 10

Program Code Output

DIM counter AS INTEGER

FOR counter = 1 TO 10

   PRINT counter

NEXT

1

2

3

4

5

6

7

8

9

10

In the example above the instruction in green: PRINT counter is executed 10 times. After each loop the value of the variable counter is incremented by 1. 

Example 2:This program print odd numbers from 1 to 10

Program Code Output

DIM counter AS INTEGER

FOR counter = 1 TO 10 STEP 2

   PRINT counter

NEXT

1

3

5

7

9

 

[ Previous Index Next ]


pages viewed since January  19 2002