Show me how to use ++ and -- on a Float temperature reading, demonstrating that floating-point types also support in-place increment and decrement.
← Operators and Expressions · Ref: Q1260
Float supports the ++ and -- operators, just like Integer. Each ++ adds 1.0 to the value; each -- subtracts 1.0. They are STATEMENT-ONLY and modify the variable in place — exactly the same semantics as Integer ++/--.
BASIC USAGE ON FLOAT
reading as Float: 23.5 reading++ // Now 24.5 reading++ // Now 25.5 reading-- // Now 24.5
The value changes by exactly 1.0 each time. There is no rounding, no precision loss beyond normal Float arithmetic.
SAMPLING SCENARIO
A temperature monitor that adjusts a setpoint by one degree at a time:
setpoint as Float: 20.0
// Three small adjustments up setpoint++ setpoint++ setpoint++ // setpoint is now 23.0
// One adjustment down setpoint-- // setpoint is now 22.0
stdout.println(`Final setpoint: ${setpoint}`)
NO EXPRESSION USE
Like Integer ++, Float ++ is statement-only. You cannot write 'next <- reading++' — that is a compile error. Increment first, then read.
WHAT IF YOU WANT FRACTIONAL INCREMENT?
Use the compound assignment += instead:
reading as Float: 23.5 reading += 0.1 // Now 23.6
The ++ operator always increments by 1.0. For other step sizes, use +=.
See Q1259 for Integer ++/--. See Q1261 for Date ++/--. See Q1262 for Enumeration ++/--. See Q241 for mutation operators overview.
Example
defines module qa.operators.incrementfloat defines program IncrementFloatDemo() stdout <- Stdout() setpoint as Float: 20.0 stdout.println(`Initial setpoint: ${setpoint}`) setpoint++ setpoint++ setpoint++ stdout.println(`After three ++: ${setpoint}`) setpoint-- stdout.println(`After one --: ${setpoint}`) reading as Float: 23.5 reading++ stdout.println(`Reading after ++: ${reading}`)
Common mistakes
E07950 — ++ is statement-only. It cannot appear inside an expression. The compiler detects the invalid expression usage. To add 2.0, use 'reading += 2.0'. See ek9 -h E07950 for details.
Incorrect:
reading: reading++ + 1.0
Correct:
reading++
Other ways to ask this
- Can I use ++ on a Float in EK9?
- Show me Float ++ and -- in action.
- Increment a Float reading in place.
- Does Float support the same ++ -- operators as Integer in EK9?
Coming from another language?
Java: float ++ supported, same statement semantics. C/C++: float ++ supported, increments by 1.0. Python: no ++ for any type. Rust: no ++ for any type, use += 1.0. Go: float ++ is statement-only. JavaScript: float ++ supported. EK9: matches the C-family conventions for Float — increment by 1.0, statement-only.
Keywords: increment, ++, in place, --, temperature, Float, decrement