Question - 2
(2)
Evaluate the given expressions:
int a =6, b=2, c=5;
c- = a++ - b++ +a-c ;
Answers
Answer:
Increment and decrement operators are unary operators that add or subtract one, to or from their operand, sequentially. They are commonly implemented in imperative programming languages like java, c, c++ the increment operator increments the value of the variable by one, and similarly, the decrement operator decrements the value of the variable by one.
a=6
a++ // a becomes 7
++a // a becomes 8
a-- // a becomes 7
--a // a becomes 6
Required Answer:-
Given:
→ a = 6, b = 2, c = 5
To Find:
→ The values of a, b and c after evaluation.
Answer:
→ a = 7, b = 3 and c = -1.
Solution:
Given that,
→ a = 6
→ b = 2
→ c = 5
Now,
→ c -= a++ - b++ + a - c
→ c = c - (a++ - b++ + a - c)
→ c = 5 - (a++ - b++ + a - 5) [As c = 5]
→ c = 5 - (6 - b++ + a - 5) [a = 6, post-increment]
→ c = 5 - (6 - b++ + a - 5) [a becomes 7]
→ c = 5 - (6 - 2 + 7 - 5) [b = 2, post-increment]
→ c = 5 - (4 + 2) [b becomes 3]
→ c = 5 - 6
→ c = -1
Therefore,
→ a = 7, b = 3 and c = -1
Refer to the attachment.