Using the Exit statement for early procedure termination
The Exit statement terminates execution of a procedure, or a Do, For, or ForAll statement, before execution reaches the end of the procedure definition or the end of the block statement.
The syntax is:
Exit exitType
exitType must be one of the keywords Do, For, ForAll, Function, Sub, or Property.
When you use Exit with a Do, For, or ForAll statement, execution continues at the first statement following the end of the block statement.
For example:
' Compute the elapsed time to execute 1000 iterations
' of a simple Do loop.
' Time may vary, depending on the workstation.
Dim doCount As Integer, startTime As Single
startTime! = Timer()
doCount% = 0
Do
' Increment doCount% through 1000 iterations of the Do loop.
doCount% = doCount% + 1
If doCount% > 1000 Then Exit Do
Loop
' Come here upon exit from the Do loop.
Print Timer() - startTime! "seconds for 1000 iterations"
' Output:
' .109375 seconds for 1000 iterations
When you use Exit with a procedure, execution continues as it would following a normal return from the procedure.
This example incorporates the Do statement from the preceding example within a sub. The Exit Sub statement terminates execution of the sub ElapsedTime after doCount% reaches 1000. Execution continues with the Print statement following the sub call. It is not necessary to terminate execution of the Do loop separately. The Exit Sub statement transfers control from the Do loop out of the sub.
' Compute the elapsed time to execute a sub that runs
' 1000 iterations of a simple Do loop.
Public startTime As Single
Sub ElapsedTime
Dim doCount As Integer
doCount% = 0
Do
doCount% = doCount% + 1
If doCount% >= 1000 Then Exit Sub
Loop
' Because of the Exit Sub statement, this Print statement
' will not be reached.
Print Timer() - startTime!, "seconds to run 1000 iterations"
End Sub
startTime! = Timer()
Call ElapsedTime()
Print Timer() - startTime! _
|seconds for sub call to run 1000 iterations|
' Output:
' .109375 seconds for sub call to run 1000 iterations
When execution continues after an Exit For statement has run, the count variable for the For statement has its most recent value, just as when execution continues after an ordinary termination of the For statement. When execution continues after an Exit ForAll statement has run, the ForAll alias variable is undefined, just as when execution continues after an ordinary termination of the ForAll statement.
Following execution of an Exit Function statement, the function returns a value to the caller. As with a normal return, this is the last value assigned before the exit. If none was assigned, the function return value is its initialized value: either 0, EMPTY, the empty string (""), or NOTHING. For example:
Function TwoVerge(seqSeed As Integer) As Single
' Leave if the call argument is not a positive integer.
' The return value of TwoVerge is its initial value, 0.
If seqSeed% < 1 Then Exit Function
TwoVerge! = Sqr(seqSeed% + 1)
Dim i As Integer
For i% = 1 To seqSeed%
' TwoVerge computes and returns a value that must be
' 1 or greater, according to the following formula.
TwoVerge! = Sqr(1 + (seqSeed% + 1 - i%) * TwoVerge!)
Next i%
End Function
Calls to TwoVerge within Print statements show the results:
Print "Seed:", -1, "Value:" TwoVerge(-1)
Print "Seed:", 20, "Value:" TwoVerge(20)
' Output:
' Seed: -1 Value: 0
' Seed: 20 Value: 1.999998