views:

55

answers:

2

 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 the C acts same as Java in this case

That is because second condition(b++ == c) would never executed if the first condition is true in 'OR' operator.

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

Thanks in advance

+9  A: 

short circuit evaluation.

cmsjr
Agreed. There is a Wikipedia article on this topic, including a table what the *short-circuit* and *greedy* logical operators are called in various programming languages: http://en.wikipedia.org/wiki/Short-circuit_evaluation#Support_in_common_programming_languages
stakx
+4  A: 

This is called sort-circuit behavior of the logical operator:

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

Source: Sun Java Tutorial

codaddict