How to bind conditional statements in Java in such a way that if b is false, then do not check c ?
b
c
If a and c are false and b is true, then c ?
a
if (a || b || c)
I am looking for a similar function that supports PHP with the difference between OR and ||
OR
||
Java operator || (logical or operator) does not evaluate additional arguments if the left operand is true . If you want to do this, you can use | (bitwise or operator), although, according to the name, this is a bit of a hack.
true
|
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.23
Thus, && computes the same result as in Boolean operands. It differs only in that the expression of the right operand is evaluated conditionally, and not always.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.24
Thus, || produces the same result as | on boolean or boolean operands. It differs only in that the expression of the right operand is evaluated conditionally, and not always.
Additionally: it is believed that a bad programming style depends on this semantics in order to conditionally invoke code with side effects.
c will not be checked. To prove this, you can use the following code:
boolean a() { System.out.println("a") return false; } boolean b() { System.out.println("b") return true; } boolean c() { System.out.println("c") return true; } void test() { if(a() || b() || c()) { System.out.println("done") } }
The output will be:
a b done
If a false and b is true, then c will not be evaluated.
See this answer for a short circuit of logical operators in Java.
1) How to link conditional statements in Java in such a way that if b is false, then do not check c ?
if (a || b)
if (a || (b && c))
2) If a and c are false and b is true, is c checked? [if (a || b || c)]
false
More on short circuit assessment: contact
for you,
a is true, then b and c are not verified.
a is false and b is true, and c is not checked.
a and b are false, then c is checked.
first evaluated the left side of the operator