if( (A) && (B) )
{
//do something
}
else
//do something else
The question is, would the statement immediately break to else if A was FA
Yes, it is called Short-circuit Evaluation.
If the validity of the boolean statement can be assured after part of the statement, the rest is not evaluated.
This is very important when some of the statements have side-effects.
In C and C++, the &&
and ||
operators "short-circuit". That means that they only evaluate a parameter if required. If the first parameter to &&
is false, or the first to ||
is true, the rest will not be evaluated.
The code you posted is safe, though I question why you'd include an empty else
block.
yes, if( (A) && (B) ) will fail on the first clause, if (A) evaluates false.
this applies to any language btw, not just C derivatives. For threaded and parallel processing this is a different story ;)
for logical && both the parameters must be true , then it ll be entered in if {} clock otherwise it ll execute else {}. for logical || one of parameter or condition is true is sufficient to execute if {}.
if( (A) && (B) ){
//if A and B both are true
}else{
}
if( (A) ||(B) ){
//if A or B is true
}else{
}
You are asking about the &&
operator, not the if
statement.
&&
short-circuits, meaning that if while working it meets a condition which results in only one answer, it will stop working and use that answer.
So, 0 && x
will execute 0
, then terminate because there is no way for the expression to evaluate non-zero regardless of what is the second parameter to &&
.