WHILE / WEND

Syntax

Syntax: WHILE condition : ... : WEND
Source: PB_STMT_WHILE handler (pb_codegen.c:6597)

Description

Repeatedly executes a block of code as long as a condition remains true. The condition is checked at the top of each iteration; if false on first entry, the body never executes. EXIT WHILE jumps out of the loop immediately. ITERATE WHILE skips the remainder of the current iteration and returns to the condition check at the top.

Parameters

ParameterDescription
conditionA boolean or comparison expression evaluated before each iteration. Non-zero = TRUE (continue), zero = FALSE (exit).
WENDMarks the end of the WHILE loop block. Execution jumps back to the WHILE condition check.

Example

FUNCTION PBMAIN() AS LONG
    LOCAL count AS LONG
    
    ' Countdown from 5 to 1
    count = 5
    WHILE count > 0
        PRINT "Count: "; count
        count = count - 1
    WEND
    PRINT "Liftoff!"
    
    ' Read until EOF (pseudocode)
    ' WHILE NOT EOF(1)
    '     LINE INPUT #1, line$
    '     PRINT line$
    ' WEND
    
    FUNCTION = 0
END FUNCTION

Generated Assembly

__pb_while_N:                         ; LOOP TOP
; Evaluate condition -> RAX
; pb_cg_emit_condition_false_jump -> jump if false
test   rax, rax
je     __pb_while_end_N               ; exit if condition false

; ... loop body ...
; EXIT WHILE jumps here: __pb_while_end_N
; ITERATE WHILE jumps here: __pb_while_N

jmp    __pb_while_N                   ; loop back
__pb_while_end_N:                     ; EXIT point

EXIT WHILE

jmp    __pb_while_end_N               ; jump out of loop

ITERATE WHILE

jmp    __pb_while_N                   ; jump to condition check