9

How to chain conditional statements in Java in a way that if b is false, than do not check c?

If a and c are false, and b is true, does c will be checked?

if (a || b || c)

I am looking for similar feature that PHP holds with difference between OR and ||

Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
  • 6
    No, || does short circuit evaluation. So if a is false, b will be checked and if b is true, c won't be checked. If you're not sure, you can always test : http://ideone.com/mCE8hy – Alexis C. Jan 18 '14 at 09:42

5 Answers5

14

The Java || operator (logical or operator) does not evaluate additional arguments if the left operand is true. If you wanted to do that, you can use | (bitwise or operator), although, according to the name, this is a little of a hack.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.23

Thus, && computes the same result as & on boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.24

Thus, || computes the same result as | on boolean or Boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.

Extra: it is considered bad programming style to depend on these semantics to conditionally call code with side effects.

Michał Zaborowski
  • 3,911
  • 2
  • 19
  • 39
Silly Freak
  • 4,061
  • 1
  • 36
  • 58
  • Sometimes it's needed if you want to squeeze performance out of your program – Szymon Toda Jan 18 '14 at 09:47
  • ah, I thought you were talking about `if(isAlright || doSomething()) ...` and not about `if(firstCondition || expensiveSecondCondition()) ...`. The first is a method with side effects, the second is just an expensive function without side effect. I'd say that the second one is fine anyway. – Silly Freak Jan 18 '14 at 09:56
  • Still it's shady and not readable-frendly but still sometimes you just have to meet the specs ;-) – Szymon Toda Jan 18 '14 at 10:05
2

c won't 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
Weibo Li
  • 3,565
  • 3
  • 24
  • 36
1

if (a || b || c)

for you case,

a is true then b and c not checked.

a is false and b is true then c not checked.

a and b are false then c is checked.

the left side of the operator is evaluated first

Rakesh Chaudhari
  • 3,310
  • 1
  • 27
  • 25
0

If a is false and b is true, then c will not be evaluated.

See this answer that addresses short circuiting of logical operators in Java.

Community
  • 1
  • 1
Roy
  • 3,574
  • 2
  • 29
  • 39
0

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

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

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

  • Nope
  • || does short circuit evaluation. So if a is false, b will be checked and if b is true, c won't be checked

For more on short circuit evaluation: refer

Community
  • 1
  • 1
Mitesh Pathak
  • 1,306
  • 10
  • 14