I want to know if condition evaluations are performed in for and while loops in Java every time a loop loop ends.
Example:
int[] tenBig = new int[]{1,2,3,4,5,6,7,8,9,10}; for(int index = 0;index < tenBig.length;index++){ System.out.println("Value at index: "+tenBig[index]); }
Will index < tenBig.length every time the loop cycle ends?
Assumption and experience say yes to me.
I know that in this example tenBig.length is constant, so there will be no performance impact.
But suppose that a condition operation takes a lot of time in another case. I know that the logical task is to assign tenBig.length variable.
However, I want to be sure that it will be evaluated every time.
java loops
Koekiebox
source share