Sunday, 13 January 2019

Groovy: Unary Arithmetic Operators


Unary Arithmetic operators are divided into four categories
a.   Unary Plus (+)
b.   Unary Minus (-)
c.    Pre, post increment operator (++)
d.   Pre, post decrement operator (--)

Unary Plus (+) and Unary Minus (-)
HelloWorld.groovy
a = +10
b = -10

println "Value of a is ${a}"
println "Value of b is ${b}"

Output
Value of a is 10
Value of b is -10

++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1

Pre increment
    Syntax:
    ++variable
      pre increment operator increments the variable value by 1 immediately.


HelloWorld.groovy
a = 10
println "++a : ${++a}"

Output
++a : 11

Post increment
    Syntax:
        variable++

 Post increment operator increments the variable value by 1 after executing the current statement.


HelloWorld.groovy
a = 10
println "a++ : ${a++}"
println "a : ${a}"

Output
a++ : 10
a : 11

Pre Decrement
    Syntax:
        --variable

 Pre decrement operator decrements the variable value by 1 immediately.


HelloWorld.groovy
a = 10
println "--a : ${--a}"

Output
--a : 9

Post Decrement
    Syntax:
        variable--

post decrement operator decrements the variable value by 1 after executing the current statement.


HelloWorld.groovy
a = 10
println "a-- : ${a--}"
println "a : ${a}"

Output
a-- : 10
a : 9


Previous                                                 Next                                                 Home

No comments:

Post a Comment