Decisions: IF and SELECT CASE
Everything after THEN on the same line belongs to the IF.
9.1 Single-line IF
Everything after THEN on the same line belongs to the IF.
IF temperature > 30 THEN PRINT "Hot!"
IF x < 0 THEN x = 0 ELSE x = x - 1
IF lives = 0 THEN PRINT "Game over": END9.2 Block IF
Rules: nothing may follow THEN on the same line for a block IF; ELSEIF is one word; close with END IF.
INPUT "Score (0-100): ", score
IF score >= 90 THEN
grade$ = "A"
ELSEIF score >= 80 THEN
grade$ = "B"
ELSEIF score >= 70 THEN
grade$ = "C"
ELSEIF score >= 60 THEN
grade$ = "D"
ELSE
grade$ = "F"
END IF
PRINT "Grade: "; grade$9.3 Nested conditions and combining tests
IF age >= 13 THEN
IF age <= 19 THEN
PRINT "Teenager"
END IF
END IF
' Same thing, flatter:
IF age >= 13 AND age <= 19 THEN PRINT "Teenager"9.4 SELECT CASE
Case forms: a value (CASE 5), a list (CASE 1, 3, 5), a range (CASE 10 TO 20), a comparison (CASE IS > 100, CASE IS <> 0), and combinations (CASE 1 TO 5, 10, IS > 50). It works with strings too:
Only the first matching CASE runs (no fall-through, no break needed). QB64 adds SELECT EVERYCASE, which runs every matching case.
INPUT "Enter a month number: ", m
SELECT CASE m
CASE 12, 1, 2
season$ = "Winter"
CASE 3 TO 5
season$ = "Spring"
CASE 6 TO 8
season$ = "Summer"
CASE 9 TO 11
season$ = "Autumn"
CASE ELSE
season$ = "Invalid month"
END SELECT
PRINT season$SELECT CASE LCASE$(cmd$)
CASE "n", "north": PRINT "You walk north."
CASE "s", "south": PRINT "You walk south."
CASE "a" TO "m": PRINT "Command starts with a letter A-M."
CASE ELSE: PRINT "I don't understand."
END SELECT9.5 Truth values in conditions
Any non-zero value is true:
Classic BASIC: only single-line IF ... THEN linenumber or IF ... THEN statements; no ELSE in many 8-bit BASICs (Commodore V2, Applesoft). Multi-way branching used ON x GOTO. FreeBASIC: If/ElseIf/Else/End If, Select Case identical; adds IIf(cond, a, b) and Select Case As Const for fast integer jump tables. VB.NET: If ... Then ... ElseIf ... Else ... End If, Select Case with Case Is > 5, Case 1 To 3; ternary If(cond, a, b); AndAlso/OrElse for short-circuiting.
found = 0
IF NOT found THEN PRINT "Not found" ' NOT 0 = -1 → true
items = 3
IF items THEN PRINT "Cart is not empty" ' 3 is non-zero → true