The cited 4 pieces of code are equivalent. Though it is thought to be trivial, I explain the reason as follows:
First, ++i is the same as i++ if we don't care the return value. Hence, 1) and 2) are equivalent. ++i has a return value of (i+1) while i++ has a return value of i. In our case,
this is irrelevant.
Second, in the for-loop control, the code in the parentheses{} is executed and next the code after the second semi-colon in the braces (). In 3), ++i is moved from within the braces into the parentheses, but the work flow is not changed. So 1) is equivalent to 3).
If one takes Sun's Java 2 programmer certificate test, he/she may meet similar question.
First, ++i is the same as i++ if we don't care the return value. Hence, 1) and 2) are equivalent. ++i has a return value of (i+1) while i++ has a return value of i. In our case,
this is irrelevant.
Second, in the for-loop control, the code in the parentheses{} is executed and next the code after the second semi-colon in the braces (). In 3), ++i is moved from within the braces into the parentheses, but the work flow is not changed. So 1) is equivalent to 3).
If one takes Sun's Java 2 programmer certificate test, he/she may meet similar question.