1.

Explain with an example the difference between ++x and x++.

Answer»

When you use ++, what you're really saying is x = x + 1.

x++ means increment x after this line, and ++x mean increment x before this line.

If you write it in its own line, then there's no real difference. However, if you use this in a line with something else then it will matter.

Code:

x = 1;

y = x++;

Means:

x = 1;

y = x;

x = x + 1;

Results:

x = 2, y = 1

Code:

x = 1;

y = ++x;

Means:

x = 1;

x = x + 1;

y = x;

Results:

x = 2, y = 2



Discussion

No Comment Found

Related InterviewSolutions