For... Next Statement

Top  Previous  Next

 

Function:

Repeats a block of statements while a counter increments until it reaches a set value.

Syntax:

 

for name = start_expression to end_expression [ step step_number ]

       statement1

       statement2

       

       [exit for]

       

       statementN

next name

Scope:

Local and HTML

See Also:

Do-Loop Statement, Exit Statement, While-Wend Statement

 


Part

Description

name

Required. The name of the counter. This has to be a numeric variable, which was previously explicitly defined using a Dim Statement.

start_expression

Required. The initial value of the counter. This actually sets the value of the name counter to the result of this expression. Can be a numerical constant, or a more complex expression.

end_expression

Required. The end value for the counter; once the counter reaches it, execution of the for... next loop stops. Can be a numerical constant, or a more complex expression.

step_number

Optional. Defines the intervals in which the name counter is incremented on every pass of the loop. This must be a numerical constant, and can be either positive or negative.

statement[1, 2...]

Required. Lines of code to be executed as long as the counter is 'in range' -- between the start_expression and the end_expression.

Details

It is not advised to change the value of the counter while within a for... next loop. You might get unexpected results, such as infinite loops.

For... next loops may be nested, using different counter variables (name above). There is also an exit statement which can be used to terminate them abruptly.

Under Tibbo Basic, you are required to explicitly state the name of the cycle variable to be incremented immediately following the next keyword.

Examples

 

dim a(10) as integer

dim f as byte

 

for f = 0 to 9 ' in a 10-member array, element indices are 0 to 9

       sum = sum + a(f)

next f