Java OR Operator Priority

How to bind conditional statements in Java in such a way that if b is false, then do not check c ?

If a and c are false and b is true, then c ?

 if (a || b || c) 

I am looking for a similar function that supports PHP with the difference between OR and ||

+6
source share
5 answers

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.

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.

+13
source

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 
+2
source

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.

0
source

1) How to link conditional statements in Java in such a way that if b is false, then do not check c ?

  • Why do you need this? he kills OR logic - neways c , then it is immaterial
  • so your answer is simply if (a || b)
  • or maybe you are looking for if (a || (b && c))

2) If a and c are false and b is true, is c checked? [if (a || b || c)]

  • Nope
  • || short circuit assessment is performed. Therefore, if a false , b will be checked, and if b true, c will not be checked

More on short circuit assessment: contact

0
source

if (a || b || c)

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

0
source

All Articles