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

C#If StatementConditional Statements

C# Problem Overview


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
}

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

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

Solution 4 - C#

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

If you use & it will be considered

Solution 5 - C#

Consider the folowing:

static int? x;
static int? y;

static void Main(string[] args)
{
    x = 5;
    if (testx() & testy())
    {
        Console.WriteLine("test");
    }
}

static Boolean testx()
{
    return x == 3;
}

static Boolean testy()
{
    return y == 10;
}

If you trace through both the testx and testy functions are evaulated even though testx was false.

If you change the test to && only the first was checked.

Solution 6 - C#

In your example, the second statement will only be evaluated if the first fails. The logical AND && will only return true when both operands are true, aka short circuit evaluation.

Solution 7 - C#

.NET supports short circuiting so when first condition goes fail, it will not check the second condtion....In C# || and && are the short-circuited versions of the logical operators | and & respectively....It is often faster too...

Solution 8 - C#

The expression will be evaluated from left to right because it is a logical AND operation. If false, it will stop evaluation. It also has the greatest precedence of the operations in your expression.

> The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary. Conditional And Operator (&&) MSDN

Here is a set of sections listing the C# operators starting with the highest precedence to the lowest.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1017882View Question on Stackoverflow
Solution 1 - C#Paul SpangleView Answer on Stackoverflow
Solution 2 - C#MBenView Answer on Stackoverflow
Solution 3 - C#Rutesh MakhijaniView Answer on Stackoverflow
Solution 4 - C#jmjView Answer on Stackoverflow
Solution 5 - C#BugFinderView Answer on Stackoverflow
Solution 6 - C#dtsgView Answer on Stackoverflow
Solution 7 - C#Akash KCView Answer on Stackoverflow
Solution 8 - C#Travis JView Answer on Stackoverflow