Placing Arithmetic Expressions in a for Statement’s Header

The initialization, loop-continuation condition and increment portions of a for statement can contain arithmetic expressions. For example, assume that x = 2 and y = 10. If x and y are not modified in the body of the loop, the statement

for (int j = x; j <= 4 * x * y; j += y / x)

is equivalent to the statement

for (int j = 2; j <= 80; j += 5)

The increment of a for statement may also be negative, in which case it’s a decrement, and the loop counts downward.

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.