Why the order of expressions in an if statement

Suppose I have a condition IF:

if (A || B) 
    |
    |
   left
{ 
   // do something  
}

Now suppose that it is Amore likely to get the true value, and then B, why does it matter to me which one is on the left?

If I put them in brackets IF, then I know (as a code programmer) that both sides are necessary.

The fact is that my professor wrote on his lecture notes that I have to put a "more probable variable in order to get true" on the left.

Can someone explain the reason? ok i put it to the left ... what am I typing? lead time?

+5
source share
7 answers

. , , .

if (s == null || s.length() == 0) // if the String is null or empty.

, NPE.

,

if (s != null && s.length() > 0) // if the String is not empty

|| false && -, . , .

+16

... ? ?

|| ++ .
 i.e: B , A a false.

, ++ "" , .

+7

javadoc

& & || - - . " " , ,

, , .

+3

, , . , . , , , , , .

+2

, , , , if. , if, , . .

, if :

if(doExpensiveCheck1() || doExpensiveCheck2()) { }

, , , .

+2

, . , (), . , , - , - , - :

(a!= nil & a.attribute == valid) {}

+2

, , , , ,

, ,

0

All Articles