Navigation:  thinBasic language > Program Flow >

FOR / NEXT

Previous pageReturn to chapter overviewNext page

 

Description

 

Executes all the statements between FOR and NEXT keywords by a number of times defined by Counter. Counter is automatically incremented at every iteration.

 

Syntax

 

FOR Counter [ AS AnyNumericType] = nStart TO nStop [STEP Increment] [{WHILE | UNTIL} LogicalExpression]

...

{statements}

[EXIT FOR]

[ITERATE FOR]

{statements}

...

NEXT [Counter]

 

Returns

 

Parameters

 

Name

Meaning

Counter

is a numeric variable controlling the iteration.

Counter variable can be created on the fly adding the optional AS clause followed by any of the basic numeric types available in thinBasic

nStart

is a numeric expression that determine the first value assigned to Counter

nStop

is a numeric expression that determine the value at which the loop will stop

Increment

is an optional numeric expression that will determine the amount of which Counter will be incremented/decremented at every iteration

LogicalExpression

Is an optional logical expression valuated as zero (%FALSE) or not zero (%TRUE) connected with WHILE or UNTIL clause to the main FOR/NEXT loop.

If present, at every FOR/NEXT iteration such expression will be evaluated and compared with the WHILE | UNTIL clause in order to check its validity. FOR/NEXT loop will terminate if Counter will reach nStop limit or this clause will be true.

 

Remarks

 

Every time a NEXT keyword is encountered, the corresponding Counter variable is incremented (or decremented) by Increment, if present, or by 1 if Increment is not present.

If LogicalExpression is present, it is evaluated before Counter variable will be incremented, permitting precise control over loop termination.

 

To exit from a FOR/NEXT loop use EXIT FOR clause. To exit from nested FOR/NEXT, multiple EXIT ... FOR can be used, one for each nesting level.

For example to exit from a 2 level FOR/NEXT use EXIT EXIT FOR. See below example.

 

At the end of a FOR / NEXT loop, Counter variable will be equal to (Stop + Increment), in case of positive Increment, or (Stop - Increment), in case of negative Increment.

If EXIT FOR is fired during a FOR / NEXT loop, at the end of the loop, Counter will have the value present just before the EXIT LOOP.

 

Restrictions

 

Counter variable after the NEXT keyword is permitted for compatibility issues with other BASIC dialects but it is just ignored by parser. You can place whatever after the NEXT statement just be aware parser will ignore it.

 

See also

 

Examples

 

Dim x As Long

Dim y As Long

Dim z As Long

 

Dim Found As Long

 

For y = 1 To 10 Until found = %True

    For x = 1 To 100 While found = %False

        For z = 1 To 1000

            If y = 2 And x = 2 And z = 900 Then

                Found = %True

                Exit For

            End If

        Next

    Next

Next

MSGBOX 0, "X, Y, Z:" & $CRLF & x & " " & y & " " & z

 

For y = 1 To 10

    For x = 1 To 100

        For z = 1 To 1000

            If y = 3 And x = 3 And z = 999 Then

                '---Exit from 3 nested level FOR/NEXT

                Exit Exit Exit For

            End If

        Next

    Next

Next

MSGBOX 0, "X, Y, Z:" & $CRLF & x & " " & y & " " & z

 

 

© 2004-2008 thinBasic. All rights reserved. Version 1.7.0.0 Web Site: http://www.thinbasic.com