Answers to Self-Study Questions


Test Yourself #2

Some examples of loops for which the compiler can be sure that the loop will execute at least once:


Test Yourself #3

As illustrated immediately below this question, a Java compiler doesn't have to give up on this optimization. A for-loop that uses literals as the low and high bounds, use i++ (where i is the loop-index variable) as the increment, and doesn't assign to i inside the loop is a perfect candidate.


Test Yourself #4

Strength reduction can still be done if the loop-index variable is incremented by any constant amount. The only change is to change

to Note that the expression increment * k1 can be evaluated at compile time, so the increment of the temporary still only involves one addition (or subtraction), and no multiplications.