The answer is ....

jabber (jabber)
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.

2000-9-27 -04:00

回到话题: Are the following 4 pieces of code equivalent to each other?

回到论坛: HOME枫下论坛枫下论坛主坛工作学习IT杂谈