Programming Fundamentals · Interview question

What's the difference between i++ and ++i?

A strong answer

Both add one to i. They differ in what the expression evaluates to. i++ (post-increment) yields the old value, then increments. ++i (pre-increment) increments, then yields the new value. As a standalone statement (i++; vs ++i;) they have identical effect. The difference matters when you embed them in a larger expression: arr[i++] accesses arr[i] and then increments i, while arr[++i] increments first and accesses arr[i+1]. For loop counters, prefer the form that makes the side effect a single line, for (int i = 0; i < n; i++) is clearer than packing it into another expression.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Operators

Everything between the variables: the symbols that combine, compare, and change values. Learn the four families and the precedence rules that bite.

More Operators questions

Browse all 472 interview questions