Groovy 6 – Math Operators

Normal Arithmetic Operators

Operator  Purpose
+addition
subtraction
*multiplication
/division
%remainder or modulus
**power

EXAMPLES

Addition

6 + 1 = 7

Subtractions

3 -2 = 1

Multiplication

2 * 2 = 4

Division

6 / 2 = 6

Remainder or Modulus

10 % 3 = 1

Power

3 ** 2 = 9

Assignment arithmetic operators

Operator  Purpose
+=Addition Assignment
-=Substation Assignment
*=Multiplication Assignment
/=Division Assignment
%=Modulus Assignment

 

 

EXAMPLES

 

 


Addition Assignment

a+=4  can be also be written as  a = a + 4

 

 

def a = 3

a+=5 (a = 3 + 5) will result in a = 8

 

 


Substation Assignment

a-=2  can be also be written as  a = a – 2

 

 

def a = 5

a-=2 (a = 5 –  2) will result in a = 3


Multiplication Assignment

a*=2  can be also be written as  a = a  *  4

 

 

def a = 2

a*=3(a = 2 * 3) will result in a = 6

 

 


Division Assignment

a/=2  can be also be written as  a = a / 2

 

 

def a = 10

a/=2 (a = 10 /  2) will result in a =5

 

 

 

 


Modulus Assignment

a%=3 can be also be written as  a = a % 3

 

 

def a = 10

a%=3 (a = 10% 3) will result in a =1

Increment/Decrement operators

Operator  Purpose
++Increment
Decrement

EXAMPLES

 

 


Increment

a++  can be also be written as  a = a + 1

 

 

def a = 10

a++ will result in 11

 

 


Decrement

a–  can be also be written as  a = a – 1

 

 

def a = 10

a– will result in 9

Leave a Reply

Your email address will not be published. Required fields are marked *