Java Assessment for Loop

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.

+6
java loops
source share
8 answers

Yes, it will logically evaluate the entire average operand at each iteration of the loop. In cases where the JIT knows better, of course, it can do smart things (even potentially removing the check of the boundaries of the array inside the loop based on the conditions of the loop).

Note that for types that the JIT does not know about, it may not be able to optimize in that way, but it may still include strings such as selecting size() ArrayList<T> .

Finally, I usually prefer an extended read loop:

 for (int value : tenBig) { ... } 

Of course, it is assumed that you do not need an index for other reasons.

+9
source share

Yes. In particular, part of the condition is satisfied before each body of the cycle. Therefore, it is possible that you will never enter the body of the cycle.

So let's take your example:

 for(int index = 0;index < tenBig.lenght;index++) { /* ...body... */ } 

This is the logical (not literal) equivalent:

 int index = 0; while (index < tenBig.length) { // happens on each loop /* ...body... */ index++; } 
+6
source share

Yes, an expression must be evaluated for each iteration of the loop to determine whether to stay inside the loop or to continue running the program.

Remember that you can do things like this:

 for(int a = 0, z = 26; a < z; a++, z--){ // Do some work } 

In this case, both sides of the expression will change and should be evaluated for each iteration.

And you are right, if you have calculations in the condition of the for loop that can be transferred to a separate variable, you should do this:

 for(int i = 0; i < ((someCount / 2) * 21.6); i++){ } 

It can be easy:

 int maxVal = (someCount / 2) * 21.6; for(int i=0; i < maxVal; i++){ { 
+3
source share

If the conditional expression is a loop invariant and you are running in JIT mode, loop optimizations such as Loop-invariant code movement can be performed.

But when running in interpreted mode, I think there is not much optimization.

+1
source share

Here are some bytecodes compiling examples from the JVM specification .

As far as I know, the condition will be evaluated every time.

Sincerely.

+1
source share

index < tenBig.length will be executed before each cycle of the cycle of the cycle.

  public static void main(String[] args) { int[] tenBig = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int index = 0; isEvaluated() && index < tenBig.length; index++) { System.out.println("Value at index: " + tenBig[index]); } } public static boolean isEvaluated() { System.out.println("evaluated"); return true; } 

It will print "evaluated" just before starting the loops. And one more time until the end of the cycle.

+1
source share

You ask, the compiler caches the value of tenBig.length as it knows that it will not change during the loop? Or you ask, does the compiler automatically know that the entire index of the expression <TenBig.length does not need to be evaluated the next 9 times?

0
source share

It will be executed every time a cycle is entered, including the last evaluation, which will give the index <length = false. In addition, although the ten-byte length is constant, the loop will always refer to this property, so it would be ideal to assign it to a variable (even if this is not a reasonable speed increase in your example).

0
source share

All Articles