0

Im trying to make an if statement like this:

if(boolean1 AND boolean2 AND boolean3 == true){
   [Do this....]
}

is there a way to do an if statement in that format or will i have to do it like this:

if(boolean1 = true & boolean2 = true ect...){}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Volken
  • 255
  • 1
  • 4
  • 23
  • 1
    btw: if you're in a boolean context, you never need to compare it to `== true`, and probably never should (it's just noise). So, don't do `if (a && b && c == true)`, just do `if (a && b && c)`. – yshavit Oct 21 '15 at 18:43
  • Also note that a single & does a bitwise and instead of a logical one. Using & when you really mean && can range from a small performance hit to disaster, depending on the instance. – Brent C Oct 21 '15 at 18:46
  • would not recommend it but `if (Stream.of(boolean1, boolean2, boolean3).allMatch(Boolean.TRUE::equals))` has semantics close to what you want. – zapl Oct 21 '15 at 19:01

4 Answers4

3

Least you can get is

if(boolean1 && boolean2 && boolean3 &&..){

}

Because since they are already booleans you need not to check for their value. Afaik, no other simple way.

If you have toooooooo many, create an array and write a simple util method to check if any is false

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

Yes, but you need two &(s) for logical and (one is a bitwise and). Also, one equals is assignment (you need two, or none). Something like,

if (boolean1 && boolean2 && boolean3){
    // ...
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 3
    Actually you don't _need_ the double `&´ (bitwise AND works fine). Though sometimes more efficient. – Mattias Isegran Bergander Oct 21 '15 at 18:45
  • That said, `&&` is much more common, so if a peer comes across a single-`&`, they're likely to assume it's a typo and fix it for you. If you really do want single-`&` for some reason, it's probably worth adding a comment in the code. – yshavit Oct 21 '15 at 18:46
  • 1
    They are not the same: the single & will check both conditions always, the double && will stop after the first condition if it evaluates to false. – Luca S. Oct 21 '15 at 19:18
0

There is no need to compare with true (though sometimes that can be nice for readability):

if (boolean1 && boolean2 && boolean3) {...
Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
0

There is no syntax that will work in quite in the fashion of checking three variables or statements against a single outcome. It's important to note that && and & as logical operators do not match all normal uses of the word AND (for instance, you could not check that boolean1 AND boolean2 AND boolean3 == false with && or & operators).

That said, evaluating boolean1 == true will give the same result as just boolean1. So the tedious if statement

if(boolean1 == true & boolean2 == true & boolean3 == true){...}

can be shortened to

if(boolean1 & boolean2 & boolean3){...}

It is also probably advisable to use && instead of & unless you have a specific reason to avoid short-circuiting. See Differences in boolean operators: & vs && and | vs || for more on that topic.

Community
  • 1
  • 1
Linus
  • 894
  • 7
  • 13