What is the ternary operator in EK9 and how do its ':' and 'else' forms differ?
← Control Flow · Ref: Q1360
EK9 has no if-EXPRESSION: 'if' is always a statement. When you want a value chosen by a condition, use the TERNARY operator (or a switch expression). The ternary is: 'condition <- valueIfTrue <separator> valueIfFalse', where the separator is either ':' or the keyword 'else' — the two forms are IDENTICAL in meaning.
TERNARY WITH ':'
passMark <- 90 grade <- score >= passMark <- "A" : "B"
Reads: if 'score >= passMark' then "A" else "B". (Comparison literals must be named — a bare 'score >= 90' is rejected with E11064 — so bind the threshold first.)
TERNARY WITH 'else'
grade <- score >= passMark <- "A" else "B"
Identical result; choose whichever reads better. 'else' is often clearer in longer expressions; ':' is more compact.
ONLY THE SELECTED ARM IS PRODUCED
The condition, and each arm, are ordinary expressions; only the chosen arm's value becomes the result.
TERNARY IS NOT COALESCING
The ternary chooses on a Boolean condition. It is distinct from the coalescing operators, which choose on set-state: '?:' (isSet coalescing, 'a ?: b' yields a if a is set else b). Use a ternary for a genuine Boolean test; use '?:' when you mean 'a, or b if a is unset'.
WHY NOT if-as-expression
'if' stays a statement so that all value production is explicit (ternary or switch), keeping branch side-effects and value-selection visually separate.
See Q61 for the if statement. See Q68 for the switch expression (the other value-producing conditional). See Q243 for coalescing operators. See Q898 for isSet vs ternary.
Example
defines module qa.controlflow.ternarycolonelse defines program TernaryColonElseDemo() stdout <- Stdout() score <- 85 passMark <- 90 // ternary with ':' — condition <- valueIfTrue : valueIfFalse grade <- score >= passMark <- "A" : "B" stdout.println(`grade (colon form): ${grade}`) // ternary with 'else' — identical meaning, different separator keyword band <- score >= passMark <- "high" else "normal" stdout.println(`band (else form): ${band}`)
Common mistakes
E02001 — EK9 has no if-expression. Use the ternary 'condition <- valueIfTrue : valueIfFalse' (or 'else' instead of ':'), or a switch expression. 'if' is a statement only. See ek9 -h E02001.
Incorrect:
grade <- if score >= 90 then "A" else "B"
Correct:
passMark <- 90 grade <- score >= passMark <- "A" : "B"
Other ways to ask this
- How do I write an if-expression in EK9?
- conditional value selection without a switch
- the ternary <- : operator
- difference between ternary ':' and 'else'
Coming from another language?
Java: 'cond ? a : b' — EK9's ':' form is the direct analogue but the condition is separated by '<-' ('cond <- a : b'). Java has no 'else' keyword variant. Python: 'a if cond else b' mirrors EK9's 'else' form. Kotlin/Rust/Swift: 'if' is itself an expression; EK9 deliberately keeps 'if' a statement and offers the ternary (and switch expression) for value selection so branch effects and value selection stay separate.
Keywords: expression, flow, select, value, else, conditional, ternary, colon, control, if-expression, coalescing