Arithmetic Operators - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Arithmetic Operators - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

## Monday, March 25, 2019

Arithmetic Operators

Subtraction                                  -
Multiplication                              *
Division                                         /
Remainder                                  %
Increment                                    ++
Subtraction Assignment              -=
Multiplication Assignment         *=
Division Assignment                    /=
Modulus Assignment                 %=
Decrement                                    --

Arithmetic Operators

Ã¼If either or both operands associated with an arithmetic operator

are floating point, the result is a floating point.

Ã¼% operator applies both to floating-point type and integer types.

Ã¼Example:

class modulus

{

public static void main (String args [])

{

int x = 42;

double y = 42.3;

System.out.println(“x mod 10 =“ + x%10);

System.out.println(“y mod 10 = “ + y%10);

}

}

Output:

x mod 10 =2

y mod 10 = 2.3

Increment and Decrement

Ã¼The increment and decrement operators are
arithmetic and operate on one operand
Ã¼The increment operator (++) adds one to its
operand
Ã¼The decrement operator (--) subtracts one from
its operand
Ã¼The statement  count++;
is functionally equivalent to   count = count + 1;
Increment and Decrement
Ã¼The increment and decrement operators can be
applied in prefix form (before the operand) or
postfix form (after the operand)

Ã¼When used alone in a statement, the prefix and
postfix forms are functionally equivalent.  That is,
count++;
is equivalent to ++count;
Increment and Decrement
Ã¼When used in a larger expression, the prefix and
postfix forms have different effects

Ã¼In both cases the variable is incremented
(decremented)

Ã¼But the value used in the larger expression depends on
the form used.

Ã¼If count currently contains 45, then the statement  total
= count++;
assigns 45 to total and 46 to count
Ã¼If count currently contains 45, then the statement  total
= ++count;
assigns the value 46 to both total and count