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.

  1. Increase and assignment operator : +=
  2. 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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.