Show me how to use the ++ and -- operators on an Integer counter, demonstrating that they are statement-only and modify the variable in place.
← Operators and Expressions · Ref: Q1259
EK9 supports ++ and -- on Integer (and many other types). They are STATEMENT-ONLY operators — they modify the variable in place and produce NO value. You cannot use them inside expressions.
BASIC USAGE
visitorCount <- 0 visitorCount++ // OK: standalone statement visitorCount++ // OK: each call increments by 1 visitorCount-- // OK: decrement by 1
WHAT YOU CANNOT DO
copy <- visitorCount++ // ERROR: ++ produces no value to assign if visitorCount++ // ERROR: ++ has no Boolean result total <- a + b++ // ERROR: ++ cannot be inside an expression
WHY STATEMENT-ONLY
In C/Java/JavaScript, ++ returns the OLD value (postfix) or NEW value (prefix). This creates subtle bugs:
x = counter++ // C: x gets old value x = ++counter // C: x gets new value
EK9 eliminates this ambiguity by making ++ purely a side-effect statement. To get the value, use the variable separately AFTER the increment.
CORRECT PATTERN
visitorCount <- 0 visitorCount++ current <- visitorCount // Read the value separately stdout.println(`After ++: ${current}`)
USAGE IN A LOOP
retries <- 0 maxRetries <- 3 while retries < maxRetries retries++ stdout.println(`Attempt ${retries}`)
This QA uses Integer specifically. See Q1260 for Float ++/--, Q1261 for Date ++/--, and Q1262 for Enumeration ++/--. The same statement-only semantics apply to every type that supports ++ and --.
See Q241 for mutation operators overview. See Q238 for the complete operator set.
Example
defines module qa.operators.incrementinteger defines program IncrementIntegerDemo() stdout <- Stdout() visitorCount <- 0 visitorCount++ visitorCount++ visitorCount++ stdout.println(`After three ++: ${visitorCount}`) visitorCount-- stdout.println(`After one --: ${visitorCount}`) retries <- 0 maxRetries <- 3 while retries < maxRetries retries++ stdout.println(`Attempt ${retries}`)
Common mistakes
E07510 — ++ is statement-only. It does not return a value, so it cannot be used as the right-hand side of an assignment. Increment first, then read the variable. See ek9 -h E07510 for details.
Incorrect:
current <- visitorCount++
Correct:
visitorCount++ visitorCount++
Other ways to ask this
- Increment and decrement an Integer using ++ and --.
- Write a small program that uses ++ to count up and -- to count down.
- Show me how Integer ++ works in EK9 — is it the same as Java?
- Demonstrate that ++ on an Integer is statement-only and not an expression.
Coming from another language?
C/C++: ++ and -- are expressions returning a value. Java: same as C, with prefix/postfix distinction. JavaScript: same. Python: no ++ or --, use += 1. Go: ++ is statement-only (no value), like EK9. Kotlin: inc()/dec() functions or += 1. Rust: no ++, use += 1. EK9: matches Go's design — statement-only, no value, no prefix/postfix ambiguity.
Keywords: decrement, in place, --, ++, increment, Integer, statement only, counter