' Conditional Patterns — simulated IIF/CHOOSE using IF/THEN
#COMPILE EXE
#DIM ALL

FUNCTION IIf(condition AS LONG, trueVal AS STRING, falseVal AS STRING) AS STRING
    IF condition THEN
        FUNCTION = trueVal
    ELSE
        FUNCTION = falseVal
    END IF
END FUNCTION

SUB SimulatedIIF()
    LOCAL i AS LONG
    LOCAL result AS STRING
    PRINT "=== SIMULATED IIF ==="
    FOR i = 1 TO 10
        IF i MOD 2 = 0 THEN result = "even" ELSE result = "odd"
        PRINT "i=" & STR$(i) & " is " & result
    NEXT i
    PRINT
END SUB

SUB InlineMaxMin()
    LOCAL a AS LONG
    LOCAL b AS LONG
    LOCAL maxVal AS LONG
    LOCAL minVal AS LONG
    PRINT "=== INLINE MAX/MIN ==="
    a = 42
    b = 17
    IF a > b THEN maxVal = a ELSE maxVal = b
    IF a < b THEN minVal = a ELSE minVal = b
    PRINT "a=" & STR$(a) & ", b=" & STR$(b)
    PRINT "Max = " & STR$(maxVal)
    PRINT "Min = " & STR$(minVal)
    PRINT
END SUB

SUB AbsValue()
    LOCAL i AS LONG
    LOCAL n AS LONG
    LOCAL absVal AS LONG
    PRINT "=== ABSOLUTE VALUE (inline) ==="
    FOR n = -5 TO 5
        IF n >= 0 THEN absVal = n ELSE absVal = -n
        PRINT "abs(" & STR$(n) & ") = " & STR$(absVal)
    NEXT n
    PRINT
END SUB

SUB ClampValue()
    LOCAL i AS LONG
    LOCAL val AS LONG
    LOCAL lo AS LONG
    LOCAL hi AS LONG
    LOCAL clamped AS LONG
    lo = 10
    hi = 90
    PRINT "=== CLAMP VALUE (10-90) ==="
    FOR i = 0 TO 100 STEP 10
        val = i
        IF val < lo THEN clamped = lo
        IF val >= lo AND val <= hi THEN clamped = val
        IF val > hi THEN clamped = hi
        PRINT "Clamp(" & STR$(val) & ") = " & STR$(clamped)
    NEXT i
    PRINT
END SUB

SUB SignFunction()
    LOCAL i AS LONG
    LOCAL n AS LONG
    LOCAL sign AS LONG
    PRINT "=== SIGN FUNCTION ==="
    FOR n = -3 TO 3
        IF n > 0 THEN sign = 1
        IF n = 0 THEN sign = 0
        IF n < 0 THEN sign = -1
        PRINT "sign(" & STR$(n) & ") = " & STR$(sign)
    NEXT n
    PRINT
END SUB

SUB SelectByRange()
    LOCAL score AS LONG
    LOCAL grade AS STRING
    PRINT "=== GRADE SELECTION ==="
    FOR score = 35 TO 100 STEP 5
        IF score >= 90 THEN grade = "A"
        IF score >= 80 AND score < 90 THEN grade = "B"
        IF score >= 70 AND score < 80 THEN grade = "C"
        IF score >= 60 AND score < 70 THEN grade = "D"
        IF score < 60 THEN grade = "F"
        PRINT "Score " & STR$(score) & " = Grade " & grade
    NEXT score
    PRINT
END SUB

SUB TernaryChain()
    LOCAL i AS LONG
    LOCAL desc AS STRING
    PRINT "=== TERNARY CHAIN ==="
    FOR i = -3 TO 3
        IF i < 0 THEN desc = "negative"
        IF i = 0 THEN desc = "zero"
        IF i > 0 THEN desc = "positive"
        PRINT STR$(i) & " is " & desc
    NEXT i
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=============================="
    PRINT " Conditional Expression Patterns"
    PRINT "=============================="
    PRINT
    CALL SimulatedIIF()
    CALL InlineMaxMin()
    CALL AbsValue()
    CALL ClampValue()
    CALL SignFunction()
    CALL SelectByRange()
    CALL TernaryChain()
    PRINT "Conditional pattern demos complete."
    FUNCTION = 0
END FUNCTION
