How a condition with multiple expression works

My "if condition" look below

if (expression1 || expression2) {
    // do something
} else {
   // do something
}

My question is if expression1 is satisfied, then the code flow goes to the else part or expression2 get checked, and then goes to else.

+4
source share
4 answers

In most languages, including Objective-C, || and && are short circuit operators . As soon as no arguments to these operators need to be checked, they are not . Therefore, if expression1 is true, the whole expression:

if (expression1 || expression2)

matches true exactly:

if(true OR X)

, X . . :

if(false || X)

X . , :

if(true && X)

X, , true. , :

if(false && X)

, , X - .

, X - , . , :

if (true || (expression2 || expression3))

(expression2 || expression3) , , , 2 3 .

+6

expression1 true, expression2 , if true , expression2.

expression1 false, expression2. expression2 false, if else

:

if (expression1){
//execute_block_1
} else if(expression2){
//execute_block_1
}else{
//execute_block_2
}
+1
$a || $b   gives  TRUE if either $a or $b is TRUE.
0

|| OR, - true, true.

if(expression1 || expression 2)

, expression1 expression2 true, true.

& is the sign of AND , if any one variable is false , then returns false . >.

if(expression1 && expression 2)

This means that if one variable expression1 or expression2 returns false , then the whole condition will return false . In this case, if both variables are true , then if the condition returns true

0
source

All Articles