4. Operators

Lesson 1

Assignment, Sign and Arithmetic Operators

Operators At a Glance

Nine operators are included in ICSE syllubus. Following are the operators in Java for ICSE:

Operators

Operators are symbols that do some kind of operations (actions) on one or more operands.
E.g.: a = 5. Here = is an operator. It assigns (stores) value 5 to the variable a. Its name is assignment operator.

Operands

Operands are variables or values on which an operation is carried out.
E.g.: In a = 5 the a and 5 are operands. The = operator operates on variable a and 5.

Expression

Expression is a valid combination of operator and operands (variable and value).
E.g.: a+5 The + is an operator. The a and 5 are operands.
The 5=a is not an expression because it is not a valid combination. The valid expression is a=5

Statement

Statement is a valid combination of tokens. It can contians all tokens or some of them.
The data type (keyword like int) is not necessary, but the ; (punctuator) is essential.
E.g.1: int a=7; (All tokens are included in it)
E.g.2: a++; This also a statement because there is ; Its meaning is a=a+1. There is = also in it.
Without ; a++ is an expression. In an expression there will not a data type and ;

Do Yourself

State whether the following are: Operator or Operand or Expression or Statement:
(1) m%2
(2) m
(3) 2
(4) m=a*b;
(5) %
(6) a/b
(7) +
(8) a=a+b;

Answers

1. Assignment Operator

The = is assignment operator; it is used to assign (store) a value to a variable. E.g.: a=5;

Do Yourself

a) Write output:

int x,y;
x=y=7+2;
System.out.println("x="+x);
System.out.println("y="+y);

b) Correct errors if any:

int m=5, n=3, s;
m+n=s;
System.out.println("Sum="+s);

c) Write output:

int a=5, b=3;
a=b;
b=a;
System.out.println("a="+a);
System.out.println("b="+b);

d) Write output:

int a=5, b=3, t; //t means temp
t=a;
a=b;
b=t;
System.out.println("a="+a);
System.out.println("b="+b);

2. Sign Operators (Unary Plus and Unary Minus)

Operators to indicate whether a number is positive and negative. E.g.: +a and -a

Write Output

int m=-4, n=6;
int x=-m*-m, y=-n*n;
System.out.print(m+" "+n);
System.out.print(x+" "+y);

3. Arithmetic Operators

Arithmetic operators are used for arithmetical operations.

Write Output

1) int a=7,b=2;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);

2) int a=2,b=4;
System.out.println(a-b);
System.out.println(a/b);
System.out.println(a%b);

3) double a=5,b=2;
System.out.println(a);
System.out.println(b);

4) double a=7,b=2;
System.out.println(a/b);
System.out.println(a%b);

5) double a=2,b=4;
System.out.println(a/b);
System.out.println(a%b);

6) int a=7,b=2;
double c=a/b;
System.out.println(c);

7) int a=2,b=4;
double c=a/b;
System.out.println(c);

Operator Precedence and Associativity

Order of evaluation among various operators is Operator Precedence and Associativity.

1. Define operator precedence with an example.
Operator precedence refers to the order of evaluation among operators in an expression with different operators.
E.g.: Among operators = + /, the / has higher precedence, next + and last is =.

Write value of a
a = 6 + 8 / 2.
a = 6 + 8 / 2
a = 6 + 4
a = 10

Write value of x
x = 10 - 3 * 2.

2. Define operator associativity with an example.
Associativity refers to the order of evaluation among operators having same precedence. The evaluation can be either from left to right or right to left. Operators * / % have same precedence. Associativity is left to right i.e., first operator executes first second one secondly … last one lastly. The + - also have same precedence, associatvity is left to right itself.

Example 1 of left to right:
Write value of a. a = 6 % 8 * 2 / 3 * 5.
a = 6 % 8 * 2 / 3 * 5
a = 6 * 2 / 3 * 5
a = 12 / 3 * 5
a = 4 * 5
a = 20

Write value of x
x = 6 % 8 * 2 / 3 * 5.

Example 2 of left to right:
Write value of a. a = 6 + 8 – 4 + 5
a = 6 + 8 - 4 + 5
a = 14 – 4 + 5
a = 10 + 5
a = 15

Write value of x
x = 8 - 6 + 5 - 4

Example of right to left:
Write value of a and b: a = b = 5+3
a = b = 5+3 At first 5+3. Then,
a = b = 8 Next b = 8. Then,
a = b i.e., a = 8, So,
a = 8, b = 8

Write value of x and y
x = y = 8-5

Use of ()

Operators inside () gets higher precedence.

Examples:
1) 4+6/2 is 7. 6/2 executes first. It is 3. Then 4+3 is 7.
2) (4+6)/2 is 5. 4+6 executes first. It is 10. Then 10/2 is 5.

Write value of m and n
m=10-6/2
n=(10-6)/2

Same or Lower Precedence Operators Before Higher Operator

When there are multiple same or lower precedence operators before higher precedence operator then except the last one all other operators execute before the higher preceence operator.

Example:
3+5+6/3
3+5+6/3
8+6/3
8+6/3
8+2
10

Result of the following calculation also same:
3+5+6/3
3+5+6/3
3+5+2
8+2
10

Write value of x

x=8-3+6-4*2

Same or Lower Precedence Operators Before Bracket

When there are multiple same or lower precedence operators before expression in bracket then except the last one all other operators execute before the operator inside bracket.

Example:
3+5-2+(7-3)
3+5-2+(7-3)
8-2+(7-3)
6+(7-3)
6+4
10

Result of the following calculation also same:
3+5-2+(7-3)
3+5-2+(7-3)
3+5-2+4
8-2+4
6+4
10

Write value of m

m=5-3+4-(6-2)

Practice Problems

a) Write value of m:
int x=8, y=3, z=2;
m=x-y*z+x-z%y/z;

b) Write values of a and b:
int a=15, b=3;
a=b=a-b*4;

c) Write values of x and y:
int a=5, b=7, c=9;
int x=a+b+c/3;
int y=(a+b+c)/3;

d) Write value of x:
int a=80,b=6,c=9,d=8,e=3,f=1,g=4;
int x = a - b * c % d / (e + f) % g

e) Write value of m:
int a=2,b=4,c=5,d=3,e=6;
int x = a+b-c+(d+a)*e/d