Will an IF statement stop evaluating if it fails the first condition?

If I have an If statement with 2 conditions - and the first fails, will the 2nd condition even be considered or will it go straight to the else? So, in the following example, if myList.Count == 0, will myString be compared against "value" or will it just straight to else?

if(myList.Count > 0 && myString.Equals("value"))
{
//Do something
}
else
{
//Do something else
}

It will stop evaluating because you're using the double ampersand && operator. This is called short-circuiting.

If you changed it to a single ampersand:

if(myList.Count > 0 & myString.Equals("value"))

it would evaluate both.


No, it will not be considered. (This is known as short circuiting.)

The compiler is clever enough (and required by language specification) to know that if the first condition is false, there is no way to expression will evaluate to true.

And as Jacob pointed for ||, when the first condition is true, the second condition will not be evaluated.


If the logical operator is AND (&&) then IF statement would evaluate first expression - if the first one is false, it would not evaluate second one. This is useful to check if variable is null before calling method on the reference - to avoid null pointer exception

If the logical operator is OR (||) then IF statement would evaluate first expression - if the first one is true, it would not evaluate second one.

Compilers and runtimes are optimized for this behavior


No, second condition will be skipped if you use &&,

If you use & it will be considered