Exit
<< Click to Display Table of Contents >> Navigation: ThinBASIC Core Language > Program Flow > Exit |
Description
Transfer program execution outside the exit specified block.
Syntax
EXIT {FOR | WHILE | DO | FUNCTION}
EXIT [(NumberOfTimes)] FOR
Returns
Parameters
Remarks
EXIT can be used multiple times with FOR statement to exit multi-level nested FOR/NEXT block. Just use multiple EXIT before the FOR statement.
For example, in the following code to exit from the inner FOR/NEXT level (3rd level) an EXIT EXIT EXIT FOR statement is used:
Dim xx As Long
Dim yy As Long
Dim zz As Long
For yy = 1 To 10
For xx = 1 To 100
For zz = 1 To 1000
If yy = 2 And xx = 2 And zz = 999 Then
Exit Exit Exit For
'Exit(3) For '---The same as above
End If
Next
Next
Next
MSGBOX 0, xx & " " & yy & " " & zz
This greatly simplify complex situations avoiding the need to declare temp control flow variable.
When exiting from FOR/NEXT blocks, an optional number of times can be indicated. This can be used to avoid to type multiple EXIT clause.
When NumberOfTimes is present, open and close parentheses must be indicated .
Restrictions
See also
Examples
Thanks to Petr Schreiber for the following script example
USES "Console"
Dim i As Long
' -- Classic usage of jumping out of DO/LOOP
Console_WriteLine "DO / LOOP exit:"
Do
INCR i
Console_WriteLine "DO -> (i=" + FORMAT$(i) + ")"
If i = 7 Then Exit Do
Console_WriteLine "LOOP <-"
Loop
Console_WriteLine "DO / LOOP exit ended..."
Console_WriteLine "Press ENTER to continue..."
Console_ReadLine
' -- Advanced jump from higher level loop
Console_WriteLine "DO / LOOP exit from nested FOR/NEXT:"
Do
Console_WriteLine "DO ->"
For i = 1 To 10
Console_WriteLine "FOR/NEXT i=" + FORMAT$(i)
If i = 5 Then Exit Do ' -- We are in FOR/NEXT block, but we can jump out from the DO/LOOP here too !
Next
Console_WriteLine "LOOP <-" ' -- This line will never be executed
Loop
Console_WriteLine "DO / LOOP exit from nested FOR/NEXT ended..."
Console_WriteLine "Press ENTER to quit..."
Console_ReadLine