a+ = a - - + ++ a + 2 * a ; if a=8, find the value of a.
Answers
Answered by
4
- The final value of a after evaluating the given expression is 40.
Given:
> a = 8
On evaluating the given expression:
> a += a-- + ++a + 2 * a
> a = a + a-- + ++a + 2 * a
> a = 8 + a-- + ++a + 2 * a
> a = 8 + 8 + ++a + 2 * a [a remains 8, post-increment]
> a = 16 + ++a + 2 * a [a becomes 7]
> a = 16 + 8 + 2 * a [a becomes 8, pre-increment]
> a = 24 + 2 * 8
> a = 24 + 16
> a = 40 (Answer)
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
Similar questions