6

How many conditions can an if check for? EG,

if(a==1 && b==1 && c==1 && ... && n==1)

I know this could probably be solved simply by nesting ifs and then it wouldn't be an issue how many. But frankly, I'm curious and can't seem to find how many it will take.

In addition, since I have your attention anyway, is there any difference in efficiency between

if(a==1 && b ==1)

And

if(a==1)
    if(b==1)
Raedwald
  • 46,613
  • 43
  • 151
  • 237
Heather T
  • 323
  • 1
  • 10
  • 20
  • 6
    Please ask one question per question. –  Feb 24 '15 at 15:40
  • Technically only 1 condition is evaluated inside an `if`, what is ascually happening is 1 condition gets evaluated and its result is evaluated with the next and so on... – Dima Maligin Feb 24 '15 at 15:41
  • 1) there is no limit. 2) both are the same – Héctor Feb 24 '15 at 15:41
  • 1
    There is no defined limit to the length of a boolean expression; likely, you will run into memory problems at some point. For your second question, see: http://stackoverflow.com/questions/5259938/what-is-better-multiple-if-statements-or-one-if-with-multiple-conditions – colti Feb 24 '15 at 15:41
  • Technically, what you have is only one conditional statement; joining multiple comparison statements with ANDs is still one conditional statement, it only varies in complexity. – ElGavilan Feb 24 '15 at 15:42
  • Thanks @ElGavilan - I corrected the wording to reflect this. – Heather T Feb 24 '15 at 15:59
  • 1
    @Poldie My apologies, but basically I was trying to see if there were any merits relative to either option respectively since it's typically nested ifs that are taught as opposed to strings of conditions. – Heather T Feb 24 '15 at 16:01

4 Answers4

8

There is no explicit limit, as far as I know. The practical limit is when readability becomes an issue.

The actual code-wise limit is probably caused by the fact that a Java method can only contain about 64 KiB of bytecode.

Your last two code blocks do the same thing (assuming there's only a single code block or statement after them). They would only be different if an else were involved.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
8

There isn't a limitation to the amounth of conditional statements in an if. Also, there's no performance improvements in either of the two blocks.

The biggest difference between the two is that you don't have control over which condition failed with the first one.

Example:

if(a==1 && b==1) {
    //do something
} else {
    //no way to tell which one failed without checking again!
}

other one:

if(a==1) {
    if(b==1) {
        //do something
    } else {
        // 'b' failed (meaning: b is not equal to 1)
    }
} else {
    // 'a' failed
}
jrtc27
  • 8,496
  • 3
  • 36
  • 68
Gerralt
  • 221
  • 2
  • 6
2

I am not sure about the quantity of the statement in an if clause but the performance between the two syntaxes is the same. The compiler generates the same bytecode no matter what syntax you use.

Kiril Aleksandrov
  • 2,601
  • 20
  • 27
2

A java if statement can test exactly one condition:

if ( boolean-expression ) statement [ else statement ]

The a==1 && b==1 && c==1 && ... && n==1 in your example is one boolean expression and, as others have already told you, there is no hard limit on how complex a boolean expression is allowed to be.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57