FOR / NEXT
<< Click to Display Table of Contents >> Navigation: ThinBASIC Core Language > Program Flow > FOR / NEXT |
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 | When} LogicalExpression]
...
{statements}
[Exit For]
[Iterate For]
{statements}
...
Next [Counter] | << In thinBasic it is not necessary to indicate counter variable into Next statement. If present it will just be skipped. |
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 or When 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 | When clause in order to check its validity.
FOR/NEXT loop will terminate if Counter will reach nStop limit or WHILE | UNTIL clause will be true.
FOR/NEXT loop will iterate if Counter has not reached nStop limit and With clause will be FALSE. |
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
This script will print on screen its source code lines only if line is not empty and is not a comment
'---This script will print on screen its source code lines only if line is not empty and is not a comment
Uses "console"
Dim sLines() As String
Dim lLine As Long
Parse(Load_File(APP_ScriptFullName), sLines(), $CRLF)
For lLine = 1 To UBound(sLines) When Len(sLines(lLine)) And LEFT$(sLines(lLine), 1) <> "'"
PrintL lLine, sLines(lLine)
Next
WaitKey
This script will print on screen a array number only if the number is equal to zero
Uses "console"
Dim A(10) As Long
Dim I As Long
'---Fill array
A(1) = 1,2,0,4,5,0,7,8,0,10
For I = 1 To UBound(a) When A(i) = 0
PrintL i, A(i)
Next
WaitKey