Q:

Java logic AND with IF

I want to perform 4 'if' statements if it can be phrased like that; I want to check four values before something happens. I have put:
if (a==1 && b==2 && c==3 && d==4) {
 foo;
}

Is stacking up logical ANDs like this the preferred method or is four individual IF statements, one inside the other preferred? The logic ANDs method seems much more efficient to me but is the a reason/time/place when they shouldn't be stacked up like this? Or is it always the preferred method?

A:

Semantically
if (a == 1 && b == 2 && c == 3 && d == 4) {
    foo;
}

is entirely equivalent to
if (a == 1)
    if (b == 2)
        if (c == 3)
            if (d == 4)
                foo;

You choose the one that looks most readable. (Note that if you have many chained conditions, the level of indentation will be terrible if you choose the latter option.)

The only reason to split it up is if you need separate else-clauses:
if (a == 1) {
    if (b == 2) {
        ....
    } else {
        // a == 1, but b != 2
    }
} else {
    // a != 1
}

A:

If the action is required if and only if all four conditions are true, then this is exactly the way to write it.

A:

whichever is more readable and understandable.
the following code
if(A)
    if(B)
        do something;
// no else

could be more understandable than if(A & B), depending on the problem. 

