## Saturday, 16 March 2013

### MCQ #8

Question) Assume x = 0 and y = 3. What is the value of x after :  x = y++;

Options)

• 0
• 3
• 4

Explanation)   You must know the difference between pre-increment (++x) and post-increment(x++)  operators before you can answer this question.

## Difference between ++x and x++

Pre-increment operator(++x) will first increment the value of x and then carry out any operations that are due whereas in post-increment(x++) operator , operation is first carried out and then x in incremented.
Sounds a bit hard to digest?
Lets take an example to ensure we understand it completely.

int x = 0;
int y = 3;

Lets consider pre-increment operator first(++y)

x = ++y;

Result of above will be x = 4 and y = 4 because pre-increment operator first increments the value i.e value becomes 5 and then carries out the = operation.

Now lets see the post-increment operator(y++)

x = y++;

Result of above will be x = 3 and y = 4 because post-increment operator first carries out the = operation i.e assigns value 3 to x and then increments the value of y to 4.

Our question represents the second case(post-increment). So our answer is 3.