EXIT / ITERATE

Syntax

EXIT: EXIT FOR | WHILE | DO | FUNCTION | SUB
ITERATE: ITERATE FOR | WHILE | DO

Description

EXIT immediately terminates the specified loop or function. EXIT FOR, EXIT WHILE, and EXIT DO break out of their respective loops. EXIT FUNCTION and EXIT SUB perform an early return from a procedure, jumping directly to the epilogue.

ITERATE skips the remainder of the current loop iteration and jumps to the next condition check. For FOR loops, it jumps to the STEP increment and comparison. For WHILE and DO, it jumps directly to the condition test.

Parameters

ParameterDescription
FORTargets the innermost enclosing FOR/NEXT loop.
WHILETargets the innermost enclosing WHILE/WEND loop.
DOTargets the innermost enclosing DO/LOOP.
FUNCTIONEXIT only. Jumps to the function epilogue, returning the current FUNCTION = value.
SUBEXIT only. Jumps to the subroutine epilogue.

Example

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    
    ' EXIT FOR: stop loop early
    FOR i = 1 TO 100
        PRINT "Checking: "; i
        IF i = 5 THEN EXIT FOR
    NEXT i
    PRINT "Stopped at: "; i
    
    ' ITERATE FOR: skip even numbers
    FOR i = 1 TO 10
        IF (i AND 1) = 0 THEN ITERATE FOR
        PRINT "Odd: "; i
    NEXT i
    
    ' EXIT FUNCTION: early return
    ' IF someError THEN EXIT FUNCTION
    
    FUNCTION = 0
END FUNCTION

EXIT

Syntax: EXIT FOR | EXIT WHILE | EXIT DO | EXIT FUNCTION | EXIT SUB
StatementGenerated ASMJumps To
EXIT FORjmp __pb_for_end_NPast NEXT
EXIT WHILEjmp __pb_while_end_NPast WEND
EXIT DOjmp __pb_do_end_NPast LOOP
EXIT FUNCTIONjmp __pb_routine_epilogue_NFunction epilogue
EXIT SUBjmp __pb_routine_epilogue_NSubroutine epilogue

ITERATE

Syntax: ITERATE FOR | ITERATE WHILE | ITERATE DO
StatementGenerated ASMJumps To
ITERATE FORjmp __pb_for_iter_NSTEP + condition check
ITERATE WHILEjmp __pb_while_NWHILE condition check
ITERATE DOjmp __pb_do_iterate_NLOOP condition check

EXIT FUNCTION (Early Return)

; When EXIT FUNCTION is encountered:
jmp    __pb_routine_epilogue_N       ; skip to epilogue

; Epilogue (always at function end):
__pb_routine_epilogue_N:
mov    rax, [rbp - return_offset]    ; load FUNCTION = value
add    rsp, frame_size
pop    rbp
ret