Java for Loop evaluation

2020-01-29 14:47发布

I want to know if the condition evaluation is executed in for and while loops in Java every time the loop cycle finishes.

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 the index < tenBig.length be execute every time the loop cycle finishes?

Assumption and experience tells me yes.

I know that in this example the tenBig.length is a constant, so there won't be a performance impact.

But lets assume that the condition operation takes long in a different case. I know the logical thing to do then is to assign the tenBig.length to a variable.

Still I want to be sure that its evaluated every time.

标签: java loops
9条回答
地球回转人心会变
2楼-- · 2020-01-29 15:01

Yes. Specifically, the condition part is executed before each loop body. So it's entirely possible that you never enter the body of the loop at all.

So taking 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++;
}
查看更多
贪生不怕死
3楼-- · 2020-01-29 15:07

Yes, it will logically evaluate the whole of the middle operand on every iteration of the loop. In cases where the JIT knows better, of course, it can do clever things (even potentially removing the array bounds check within the loop, based on the loop conditions).

Note that for types that the JIT doesn't know about, it may not be able to optimize specifically like this - but may still inline things like fetching the size() of an ArrayList<T>.

Finally, I generally prefer the enhanced for loop for readability:

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

Of course, that's assuming you don't need the index for other reasons.

查看更多
孤傲高冷的网名
4楼-- · 2020-01-29 15:11

Short Answer: the condition is evaluated every time so:

WRONG:

for (int i = 0; i < Math.random() * 50; i++) {
    System.out.println("" + i);
}

CORRECT:

double random = Math.random();
for (int i = 0; i < random * 50; i++) {
    System.out.println("" + i);
}
查看更多
小情绪 Triste *
5楼-- · 2020-01-29 15:12

index < tenBig.length will be execute before every time the loop cycle starts.

    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 cycles starts. And one more time before loop finishes.

查看更多
Ridiculous、
6楼-- · 2020-01-29 15:15

Here are several bytecode compiling examples from the JVM spec.

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

Regards.

查看更多
该账号已被封号
7楼-- · 2020-01-29 15:15

It's going to be executed everytime the loop is entered, including the last evaluation which will yield index < length = false . Aside from this, even though the length of tenBig is const, the loop will always access this property so it would be ideal to assign it to a variable (even though it's not a reasonable speed gain in your example).

查看更多
登录 后发表回答