Why can't I use ++ or -- in an expression in EK9?
← Operators and Expressions · Ref: Q780
In EK9, ++ and -- are STATEMENT-ONLY operators. They modify a variable in place and return nothing. You cannot use them where a value is expected (assignments, conditions, function arguments).
WHY STATEMENT-ONLY
In C/Java, 'y <- x++' has confusing semantics: y gets the OLD value before increment (C) or becomes an alias to x (Java). Both are bug sources. EK9 eliminates this by making ++ and -- void operations.
CORRECT USAGE
count++ // OK — standalone statement count-- // OK — standalone statement
INCORRECT USAGE
y <- count++ // ERROR — ++ returns void, cannot assign if count++ // ERROR — ++ returns void, not Boolean
IF YOU NEED THE OLD VALUE
oldCount <- count count++ // Now oldCount has the previous value
See Q238 for operator overview. See Q239 for comparison operators.
Example
defines module qa.operators.incrementstatement defines program CountItems() stdout <- Stdout() count <- 0 count++ count++ count++ total <- count stdout.println($total)
Common mistakes
E07950 — Using ++ in an assignment expression is not allowed. The ++ operator is statement-only — it modifies the variable and returns void. Increment first, then assign separately. See ek9 -h E07950 for details.
Incorrect:
total <- count++
Correct:
count++ total <- count
Other ways to ask this
- What triggers E07950 increment decrement expression not allowed?
- Why is y <- x++ an error in EK9?
- How do I increment a variable in EK9?
Coming from another language?
Java: x++ returns old value (post-increment), ++x returns new value (pre-increment). C/C++: same as Java plus undefined behavior in complex expressions. Python: no ++ or -- operators at all. Rust: no ++ or -- operators. Go: ++ and -- are statements only (same as EK9). Kotlin: ++ and -- work as expressions. EK9: ++ and -- are statements only, like Go.
Keywords: increment, bug, assign, void, expression, operator, E07950, statement, decrement