If statement with logical OR


 public class Test{
      public static void main(String args[]){
    int a = 0;
    int b = 1;
    int c = 10;
    if ( a == 0 || b++ == c ){
        a = b + c;
    }else{
        b = a + c;
    }
    System.out.println("a: " + a + ",b: " + b + ",c: " + c);
    }
}

Ok, this is Java code, and the output is a: 11, b: 1, c: 10 And I believe that C acts the same as Java in this case

This is because the second condition (b ++ == c) will never be met if the first condition is true in the OR operator.

There is "NAME" for this. I just don’t remember what it is. Does anyone know what this is called?

+3
source share
2 answers

short circuit rating.

+10
source

This is called the short-circuitbehavior of the logical operator:

In versions with a short circuit, the evaluation of subsequent subexpressions is canceled as soon as the subexpression evaluates to false (in the case & &) or true (in the case ||).

+5

Source: https://habr.com/ru/post/1739610/


All Articles