Tricky Questions

Difference between i++ and ++i

Both of them will add 1 to a numeric value but return different results.

i++ returns the original value (before increment).

++i returns the incremented value.

Here's an example:


Line 4 shows the main difference between post and pre increment - the returned result

The same logic applies to i-- and --i.

Semicolon after 'for' loop

For loop with no body will still repeat many times as long as it's condition is met.

This means that variable's modifications such as i++, i-- are still performed.

Let's look at the example:


In this example the loop repeats 5 times until i reaches value of 5.