12

This may actually be a silly question but I am wondering if it is possible to have an if statement executing all conditions. I explain:

if (methodA() && methodB() && methodC()) {code}

code is executed when all three methods return true. The point is that when a method returns false, the rest of the methods aren't executed. This is normally a good thing for performance but what if I really need to execute all methods independently of what they are returning and after that evaluate the expression and go into the if or not. The same is applied for OR or whatever

Is there a way to tell java to behave that way? My current work around is to split it in three ifs but this not always does the trick and looks really ugly.

iberbeu
  • 15,295
  • 5
  • 27
  • 48

9 Answers9

29

That’s quite simple: use the & operator instead of &&.

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

The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.

Holger
  • 285,553
  • 42
  • 434
  • 765
9

Use the bitwise & operator, which evaluates both sides of the condition and stops the conditional from short circuiting. As you have noticed the && and || conditional operators will short circuit once the result of the expression can be determined.

if (methodA() & methodB() & methodC()) {code}

Documentation

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
3

try this

& is bitwise. && is logical.

 & evaluates both sides of the operation.
 && evaluates the left side of the operation, if it's true, it continues and evaluates the right side.

for fast execution use && if (methodA() & methodB() & methodC()) {code}

Invader
  • 679
  • 3
  • 10
2

You should use bitwise AND (&) operator instead of logical AND (&&).

if (methodA() & methodB() & methodC()) {code}

More detail read here.

Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
2

Your exact answer is:

if (methodA() & methodB() & methodC()) {code}
Peter_James
  • 647
  • 4
  • 14
0

Use Bitwise AND instead of &&

   if (methodA() & methodB() & methodC()) {code}
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
0

Just change && to &. So your answer is

if (methodA() & methodB() & methodC()) {code}

&&is a comparison. It means the if statement will check the methodA() && methodB() conditional first. If this return false, then the methodC() will not execute. If true, then it will check with the methodC() function.

& is a bitwise. It will return true if all the value is 1. methodA() & methodB() & methodC() count as one comparison. So it will run through all the function. And then check if the answer is true or false.

david
  • 3,225
  • 9
  • 30
  • 43
0

Another solution (but not that elegant) would be to use boolean variables:

boolean resA = methodA();
boolean resB = methodB();
boolean resC = methodC();

then do your ifs:

if (resA && resB && resC) { code }

That way your methods will run, and you just need to compare what they returned.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
0

For kotlin users

if (methodA() and methodB() and methodC()) {code}