int a=11 ; b =22 , c;
c= a + b + a++ + b++ + ++a + ++b
System. out. println ( a+b+c+34)
Answers
- The output of the given code is 174.
Given:
> a = 11
> b = 22
On evaluating the value of c:
> c = a + b + a++ + b++ + ++a + ++b
> c = 11 + 22 + a++ + b++ + ++a + ++b [Substitute the values of a and b]
> c = 33 + 11 + b++ + ++a + ++b [a remains 11, post-increment]
> c = 44 + b++ + ++a + ++b [a becomes 12]
> c = 44 + 22 + ++a + ++b [b remains 22, post-increment]
> c = 66 + ++a + ++b [b becomes 23]
> c = 66 + 13 + ++b [a becomes 13, pre-increment]
> c = 79 + ++b
> c = 79 + 24 [b becomes 24, pre-increment]
> c = 103
Therefore:
Now, the value of a + b + c + 34 will be,
= 13 + 24 + 103 + 34
= 37 + 34 + 103
= 71 + 103
= 174
★ So, the output of the given code is 174.
There are two types of increment/decrement operations.
- Post increment/decrement.
- Pre increment/decrement.
Post Increment/Decrement: Here, operation takes place first and then the value is incremented/decremented.
Example:
> int a = 2;
> int b = a++;
> b = 2
Now, a = 3.
Pre Increment/Decrement: Here, value is first increment/decrement and then the operation is carried out.
Example:
> int a = 2;
> int b = ++a;
> b = 3
Also, a = 3