IF / ELSE / ELSEIF / END IF

Syntax

Syntax: IF condition THEN stmts [ELSEIF cond THEN stmts] [ELSE stmts] END IF
Source: pb_cg_emit_condition_false_jump (pb_codegen.c:305)

Description

Conditionally executes blocks of code based on a boolean or comparison expression. The IF statement is the primary branching construct. The compiler evaluates the condition and generates a conditional jump to skip the THEN block when the condition is false. ELSEIF chains multiple conditions efficiently, and ELSE provides a default path. Supports both integer and floating-point comparisons with all standard relational operators.

Parameters

ParameterDescription
conditionA comparison (x < 10) or boolean expression. Non-zero evaluates as TRUE, zero as FALSE.
THENIntroduces the block executed when the condition is true. Can be followed by a single statement on the same line or a multi-line block.
ELSEIF condition THENTests an additional condition if all previous conditions were false. Multiple ELSEIF blocks are allowed.
ELSEOptional block executed when no condition is true.
END IFTerminates the IF block. Required for multi-line IF statements.

Example

FUNCTION PBMAIN() AS LONG
    LOCAL score AS LONG
    score = 85
    
    IF score >= 90 THEN
        PRINT "Grade: A"
    ELSEIF score >= 80 THEN
        PRINT "Grade: B"
    ELSEIF score >= 70 THEN
        PRINT "Grade: C"
    ELSE
        PRINT "Grade: F"
    END IF
    
    FUNCTION = 0
END FUNCTION

Generated Assembly - Integer Comparison

; Evaluate left expression -> RAX, save to temp
; Evaluate right expression -> RAX
mov    r10, rax
mov    rax, [rbp - left_offset]       ; reload left
cmp    rax, r10                       ; compare left, right
je     __pb_else_N                    ; jump to else if condition FALSE
; ... THEN body ...
jmp    __pb_endif_N
__pb_else_N:
; ... ELSE body ...
__pb_endif_N:

Integer Jump Mappings

OperatorJump to ELSE (condition FALSE)Meaning
= (EQ)jneJump if not equal
<> (NE)jeJump if equal
< (LT)jgeJump if greater or equal
<= (LE)jgJump if greater
> (GT)jleJump if less or equal
>= (GE)jlJump if less

Float Comparison

; Evaluate left -> XMM0, save to [rbp - temp]
movsd  xmm1, [rbp - temp]             ; reload left
ucomisd xmm1, xmm0                    ; unordered compare
ja     __pb_else_N                    ; jump based on flags

Float Jump Mappings

OperatorJump to ELSE
=jne
<>je
<jae
<=ja
>jbe
>=jb

Boolean Truthiness Test

; Evaluate expression -> RAX (0 = false, non-0 = true)
test   rax, rax
je     __pb_else_N                    ; jump if 0 (false)

ELSEIF Expansion

; IF a THEN      -> cmp + jne __pb_elseif1_N
; ELSEIF b THEN  -> __pb_elseif1_N: cmp + jne __pb_else_N
; ELSE           -> __pb_else_N:
; END IF         -> __pb_endif_N: