Home »
Scala
Replacements for ++ and -- in Scala (increment or decrement an integer)
By IncludeHelp Last updated : October 09, 2024
Scala has no ++ or -- operator, how to increment or decrement an integer?
The Scala programming language doesn't support unary operators (++ or --). In Scala, the binary operators are used to increment and decrement an integer.
Common binary operators for increasing and decreasing an integer, we use the increase and assign or decrease and assign operator.
- Increase and assignment operator : +=
- Decrease and assignment operator : -=
Scala example of increment or decrement an integer
object myClass {
def main(args: Array[String]): Unit = {
var a = 45;
println("initial value of a " + a)
// incrementing the value
a += 1
println("incremented value of a " + a)
// decreasnig the value
var b = 78
println("initial value of b " + b)
b -= 1
println("decremented value of b " + b)
}
}
Output
initial value of a 45
incremented value of a 46
initial value of b 78
decremented value of b 77