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

If I have an If statement with 2 conditions - and the first one fails, will the second condition even be considered or will it be directly on else ? So, in the following example, if myList.Count == 0 , will myString be compared with the value "value" or will it just be right on the else ?

 if(myList.Count > 0 && myString.Equals("value")) { //Do something } else { //Do something else } 
+42
c # if-statement conditional-statements
Jul 06 '12 at 8:28
source share
8 answers

It will stop evaluating because you are using a double ampersand && operator. This is called short-circuiting .

If you changed it to one ampsersand:

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

he will evaluate both.

+65
Jul 06 2018-12-12T00:
source share

No, this will not be considered. (This is called a short circuit .)

The compiler is smart enough (and required by the language specification) to know that if the first condition is false , there is no way to express an expression for true .

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

+16
Jul 06 '12 at 8:29
source share

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

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

Compilers and runtimes are optimized for this behavior.

+7
Jul 06 '12 at 8:32
source share

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

If you use & , it will be considered

+3
Jul 06 '12 at 8:29
source share

Consider the following:

 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 both the testx and testy functions will be evaulated, although testx was false.

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

+2
Jul 06 2018-12-12T00:
source share

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

+1
Jul 06 '12 at 8:30
source share

.NET supports short circuiting , so when the first condition fails, it will not check the second situation ... In C # || and && are short-circuited versions of logical operators | and accordingly .... This is often faster ...

+1
Jul 6 2018-12-12T00:
source share

The expression will be evaluated from left to right, because it is a logical operation I. If false, it will stop evaluating. It also has the highest priority of operations in your expression.

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

Here is a set of sections that list C # statements, from the highest priority to the lowest .

0
Aug 22 '17 at 19:05
source share



All Articles