Blitz3D+ Command Reference

Exit

Parameters

None.

Description

Leaves the innermost loop immediately.

Exit works in all four loop shapes - For ... Next, For Each, While ... Wend and Repeat ... Until. Execution carries on at the first statement after the loop's closing line, skipping the rest of the current pass and any passes that were still to come.

The classic use is a search: walk a list, and the moment you find what you were after, stop looking. It is just as useful in a game loop, where Exit is how the player quits - If KeyHit(1) Then Exit.

It only ever leaves one level. From a loop inside a loop, Exit drops you into the outer loop, not out of both, so a nested search usually wants a flag variable the outer loop checks as well.

Exit outside any loop is a compile error, not a silent no-op. To leave a Function early use Return, and to stop the program altogether use End.

See also: For, While, Repeat, Return, End.

Example

; Exit Example
; ------------

; The treasure is hidden in crate 4
treasure=4

; Search the crates - Exit leaves the loop the moment we find the loot
For crate=1 To 10
    Print "Opening crate "+crate+" ..."
    If crate=treasure Then
        Print "    Treasure found! No need to keep searching."
        Exit
    End If
Next

Print ""
Print "The search stopped early - crates 5 to 10 were never opened."

Print ""
Print "Press any key to close the example"
WaitKey

End

Index