How many conditions can be checked if

How many conditions can a check fulfill? EG,

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

I know that this could probably be solved simply by nesting the ifs, and then that would not be a problem. But to be honest, I'm curious, and I can’t find how much it will take.

Also, since I have your attention anyway, is there a difference in efficiency between

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

and

 if(a==1) if(b==1) 
+5
source share
4 answers

There are no restrictions on the expression of conditional statements in if. In addition, there is no performance improvement in either of the two blocks.

The biggest difference between the two is that you do not have control over which condition did not work with the first.

Example:

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

other:

 if(a==1) { if(b==1) { //do something } else { // 'b' failed (meaning: b is not equal to 1) } } else { // 'a' failed } 
+7
source

I do not know how much I know. The practical limit is when readability becomes a problem.

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

Your last two code blocks do the same (assuming there is only one code block or statement after them). They would be different if else were involved.

+8
source

I'm not sure how many instructions there are in the if clause, but the performance between the two syntaxes is the same. The compiler generates the same bytecode no matter what syntax you use.

+2
source

The java if can test only one condition:

if ( boolean-expression ) statement [ else statement ]

a==1 && b==1 && c==1 && ... && n==1 in your example is one logical expression, and as others have told you, there is no hard limit on how complicated it can be boolean expression.

+2
source

Source: https://habr.com/ru/post/1214091/


All Articles