简体   繁体   中英

Java short circuit confusion

This code uses && in the for loop condition. It iterates 4 times resulting in the answer "sum = 20". I would think it iterates 5 times, since the left side of the && condition is true, when the right side becoming false ends the loop.

Basically my question is why does it iterate 4 times and not 5, making the "sum = 30"? Thanks

   `int[] lst = {1,2,3,4,5,4,3,2,1};
    int sum = 0;
    for (int frnt = 0, rear = lst.length - 1;
            frnt < 5 && rear >= 5; frnt++, rear--){
        sum = sum + lst[frnt] + lst[rear];
        System.out.println("frnt: " + frnt);
        System.out.println("rear: " + rear);
        System.out.println();
    }
    System.out.print(sum);`

Short circuiting only happens when the value of the whole expression is already known.

For the Logical AND operator (&&) if the left hand side is already false, then it does not matter what the right hand side evaluates to because the whole expression is already false. If the left hand side is true, then the value of the right side determines the value of the expression. Since it is not known until it is evaluated, it must be evaluated.

For the Logical OR operator (||), the reverse is true. If the left side of the expression is true, then it doesn't matter what the right side is, the expression is true, so the right side is not evaluated. If the left side is false, then the value of the expression depends on the right side so it must be evaluated.

The problem is originating within the for condition. On the last iteration of the loop, both frnt = 4 and rear = 4 . The condition frnt < 5 works, but rear >= 5 is false, making the entire expression false, which breaks the loop. See below for the variables after each iteration (Note the table shows the variabls after each time the body of the loop has ran and the variables have been modified).

              frnt   rear
before loop:  0      8
iteration 1:  1      7
iteration 2:  2      6
iteration 3:  3      5
iteration 4:  4      4 <---- Loop breaks after this

The two counters meet in the middle at 4, but the condition specifies that rear >= 5 . In order to get the correct behavior, change the condition to: frnt < 5 && rear >= 4 , or more clearly, frnt <= 4 && rear >= 4

frnt ---- rear ---------- frnt < 5 && rear >= 5

0 ----------- 8 ------------------- true && true -> true

1 ----------- 7 ------------------- true && true -> true

2 ----------- 6 ------------------- true && true -> true

3 ----------- 5 ------------------- true && true -> true

4 ----------- 4 ------------------- true && false -> false

Everything is right, 4 iterations and that's it.

I think you misunderstood the short-circuit principle. In case of && operator the short circuit principle works as follows:

As the result of A && B operation is true only in case when both operands are true then upon evaluating operand A and discovering that it is false , there is no need to evaluate and check the value of operand B. In case when operand A is true operand B is always evaluated. So in your case upon the check when the loop breaks the operand A is 4 < 5 and the operand B is 4 >= 5 . So as the operand A is true then it is mandatory for operand B to be evaluated and checked and this case is not short circuit as both operands are evaluated. Short circuit case would be the one if operand A is false and operand B is not evaluated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM