Page 160 - ComputerScience_Class_11
P. 160
Given below is the example of the prefix decrement operator. Here, the decrement by 1 is executed first and then the
value is used.
int a=5, b;
System.out.println("a: " +a);
b=--a;
System.out.println("a: " +a+ " and b: " +b);
Output:
a: 5
a: 4 and b: 4
The postfix increment/decrement operator works on the principle of ‘Change After Action’.
Given below is an example of the postfix increment operator. Here, the value is used first and then an increment by
1 is done on the given value.
int a=5, b;
System.out.println("a: " +a);
b=a++;
System.out.println("a: " +a+ " and b: " +b);
Output:
a: 5
a: 6 and b: 5
Given below is an example of the postfix decrement operator. Here, the value is used first and then a decrement by
1 is done on the given value.
int a = 5, b;
System.out.println("a: " +a);
b = a--;
System.out.println("a: " +a+ " and b: " +b);
Output:
a: 5
a: 4 and b: 5
Some more examples are as follows:
1. If int n1 = 5, n2 = 6, s;
s = n1++ + ++n2;
Ans. 12 Working: 5 + 7 = 12
2. If int a = 50, c;
c = a++ + ++a / 4;
Ans. 63 Working: 50 + 52 / 4 = 50 + 13 = 63
3. If int m = 12;
m = m++ / ++m * m;
Ans. 0 Working: 12 / 14 * 14 = 0 * 14 = 0
4. If int k = 10, l = 20, m;
m = ++k + --l;
Ans. 30 Working: 11 + 19 = 30
158 Touchpad Computer Science (Ver. 3.0)-XI

