Why incrementing a counter in a for's body rather than in the header?

132 Views Asked by At

I've found the following piece of code in java.util.Calendar:

public final void clear()
{
    for (int i = 0; i < fields.length; ) {
        stamp[i] = fields[i] = 0; // UNSET == 0
        isSet[i++] = false;
    }
    areAllFieldsSet = areFieldsSet = false;
    isTimeSet = false;
}

I understand what it's doing and why it's working. But missing the increment and doing it at the end of the loop, it clearly differs from the "normal" for pattern (as described in http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html)

for (initialization; termination; increment) {
    statement(s)
}

Is there any advantage in the approach I've shown above?

1

There are 1 best solutions below

0
On BEST ANSWER

No. If the addition was prefix instead of postfix, it would make sense since the meaning would change entirely. In this particular case, there does not even seem to be any reason in terms of optimization. As per the comments, this is probably just a case of hasty coding.