' Short-Circuit Evaluation — AND/OR behavior demos
#COMPILE EXE
#DIM ALL

FUNCTION IsPositive(n AS LONG) AS LONG
    PRINT "  IsPositive(" & STR$(n) & ") called"
    FUNCTION = (n > 0)
END FUNCTION

FUNCTION IsEven(n AS LONG) AS LONG
    PRINT "  IsEven(" & STR$(n) & ") called"
    FUNCTION = (n MOD 2 = 0)
END FUNCTION

FUNCTION IsInRange(n AS LONG, lo AS LONG, hi AS LONG) AS LONG
    PRINT "  IsInRange(" & STR$(n) & "," & STR$(lo) & "," & STR$(hi) & ") called"
    FUNCTION = (n >= lo AND n <= hi)
END FUNCTION

SUB ShortCircuitAND()
    LOCAL x AS LONG
    LOCAL y AS LONG
    PRINT "=== SHORT-CIRCUIT AND ==="
    PRINT "AND with first operand FALSE (short-circuits):"
    x = -5
    PRINT "x = " & STR$(x)
    IF IsPositive(x) AND IsEven(x) THEN
        PRINT "  Both true"
    ELSE
        PRINT "  Result: FALSE (IsEven NOT called - short circuit)"
    END IF
    PRINT
    PRINT "AND with first operand TRUE (evaluates both):"
    x = 4
    PRINT "x = " & STR$(x)
    IF IsPositive(x) AND IsEven(x) THEN
        PRINT "  Result: TRUE (both called)"
    ELSE
        PRINT "  Result: FALSE"
    END IF
    PRINT
END SUB

SUB ShortCircuitOR()
    LOCAL x AS LONG
    PRINT "=== SHORT-CIRCUIT OR ==="
    PRINT "OR with first operand TRUE (short-circuits):"
    x = 7
    PRINT "x = " & STR$(x)
    IF IsPositive(x) OR IsEven(x) THEN
        PRINT "  Result: TRUE (IsEven NOT called - short circuit)"
    ELSE
        PRINT "  Result: FALSE"
    END IF
    PRINT
    PRINT "OR with first operand FALSE (evaluates both):"
    x = -3
    PRINT "x = " & STR$(x)
    IF IsPositive(x) OR IsEven(x) THEN
        PRINT "  Result: TRUE"
    ELSE
        PRINT "  Result: FALSE (both called)"
    END IF
    PRINT
END SUB

SUB GuardPattern()
    LOCAL arr(1 TO 5) AS LONG
    LOCAL i AS LONG
    LOCAL idx AS LONG
    arr(1) = 10
    arr(2) = 20
    arr(3) = 30
    arr(4) = 40
    arr(5) = 50
    PRINT "=== GUARD PATTERN (safe array access) ==="
    idx = 3
    IF idx >= 1 AND idx <= 5 THEN
        PRINT "arr(" & STR$(idx) & ") = " & STR$(arr(idx))
    END IF
    idx = 10
    IF idx >= 1 AND idx <= 5 THEN
        PRINT "arr(" & STR$(idx) & ") = " & STR$(arr(idx))
    ELSE
        PRINT "Index " & STR$(idx) & " out of range (guarded)"
    END IF
    PRINT
END SUB

SUB NestedShortCircuit()
    LOCAL a AS LONG
    LOCAL b AS LONG
    LOCAL c AS LONG
    PRINT "=== NESTED SHORT-CIRCUIT ==="
    a = 1
    b = 0
    c = 5
    PRINT "a=" & STR$(a) & ", b=" & STR$(b) & ", c=" & STR$(c)
    IF (a > 0 AND b <> 0) OR c > 3 THEN
        PRINT "Complex expression TRUE"
    ELSE
        PRINT "Complex expression FALSE"
    END IF
    PRINT
    PRINT "Division guard (prevent divide by zero):"
    b = 0
    IF b <> 0 AND (a / b) > 0 THEN
        PRINT "  Division safe (but short-circuited)"
    ELSE
        PRINT "  Division guarded - b is zero"
    END IF
    PRINT
END SUB

SUB EfficientCheck()
    LOCAL i AS LONG
    LOCAL found AS LONG
    DIM data(1 TO 10) AS LONG
    FOR i = 1 TO 10
        data(i) = i * 3
    NEXT i
    PRINT "=== EFFICIENT SEARCH (early exit via AND) ==="
    found = 0
    FOR i = 1 TO 10
        IF found = 0 AND data(i) = 15 THEN
            found = i
        END IF
    NEXT i
    PRINT "First occurrence of 15 at index: " & STR$(found)
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "==============================="
    PRINT " Short-Circuit Evaluation Demo"
    PRINT "==============================="
    PRINT
    CALL ShortCircuitAND()
    CALL ShortCircuitOR()
    CALL GuardPattern()
    CALL NestedShortCircuit()
    CALL EfficientCheck()
    PRINT "Short-circuit demos complete."
    FUNCTION = 0
END FUNCTION
