简体   繁体   English

Java短路混乱

[英]Java short circuit confusion

This code uses && in the for loop condition. 此代码在for循环条件中使用&&。 It iterates 4 times resulting in the answer "sum = 20". 迭代4次,得出答案“ 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. 我认为迭代5次,因为&&条件的左侧为true,当右侧变为false时,结束循环。

Basically my question is why does it iterate 4 times and not 5, making the "sum = 30"? 基本上我的问题是为什么要迭代4次而不是5次,使“和= 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. 对于逻辑AND运算符(&&),如果左侧已经为假,则右侧求值的结果无关紧要,因为整个表达式已经为假。 If the left hand side is true, then the value of the right side determines the value of the expression. 如果左侧为true,则右侧的值将确定表达式的值。 Since it is not known until it is evaluated, it must be evaluated. 由于直到对其进行评估才知道,因此必须对其进行评估。

For the Logical OR operator (||), the reverse is true. 对于逻辑OR运算符(||),反之亦然。 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. 如果表达式的左边为true,那么右边的表达式为true并不重要,因此不会对右边求值。 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. 问题出在for条件内。 On the last iteration of the loop, both frnt = 4 and rear = 4 . 在循环的最后一次迭代中, frnt = 4frnt = 4 rear = 4 The condition frnt < 5 works, but rear >= 5 is false, making the entire expression false, which breaks the loop. 条件frnt < 5有效,但rear >= 5为假,使整个表达式为假,这破坏了循环。 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 . 这两个计数器在中间的4点相遇,但条件指定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 < 5 && rear >= 4 ,或者更清楚地, frnt <= 4 && rear >= 4

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

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

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

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

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

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

Everything is right, 4 iterations and that's it. 一切都正确,只需4次迭代就可以了。

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. 因为A && B运算的结果为true只有当两个操作数是区分true那么在评估数A,发现它是false ,没有必要评估和检查操作数B的值如果在操作数A为始终对true操作数B求值。 So in your case upon the check when the loop breaks the operand A is 4 < 5 and the operand B is 4 >= 5 . 因此,在您检查循环中断时的情况下,操作数A为4 < 5 ,操作数B为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. 因此,由于操作数A为true ,因此必须对操作数B进行评估和检查,并且这种情况并非短路,因为两个操作数均被评估。 Short circuit case would be the one if operand A is false and operand B is not evaluated. 如果操作数A为false且未评估操作数B,则为短路情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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