Home »
Scala language
Scala has no ++ or – operator, how to increment or decrement an integer?
Increment and decrement operations in Scala: In Scala, the unary increment (++) and decrement (--) operators are not valid. So, we have to use binary operators to fulfill our purpose. In this tutorial, we will learn about Scala binary increment and decrement operators?
Submitted by Shivang Yadav, on August 01, 2019
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 : -=
Working example:
object myClass{
def main(args: Array[String]){
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