a.sapp a.sapp - 3 years ago 209
Java Question

Java for-loop going past exit condition

I have a for-loop that is running through an array of size 512 (index range 0 - 511). However, when i == 512, it continues into the loop anyways throwing an ArrayIndexOutOfBounds exception. The screenshot below shows my code when I run it in debug mode with the stop condition set to i == 512.

enter image description here

Anyone have any idea why this loop is continuing to cycle even after the exit condition is met?

Answer Source

Your loop condition is:

i < frameSet * associativity + associativity

According to the debugger, the current values are 512 < 64 * 8 + 8 or 512 < 520, which evaluates to true. That is why the loop is still running.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download