C# && VS & and || VS |

We already learned Logical Operators in the previous sections. In this section, we'll compare the operators in If-Else statement.

Example 01-29-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;

namespace TestLogicalAnd
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0, j = 0;

            if( ++i > 1 & ++j > 1  ){
                Console.WriteLine("Condition=true: i={0}, j={1}", i, j);
            }else{
                Console.WriteLine("Condition=false: i={0}, j={1}", i, j);
            }

            i = 0;
            j = 0;
            if( ++i > 1 && ++j > 1  ){
                Console.WriteLine("Condition=true: i={0}, j={1}", i, j);
            }else{
                Console.WriteLine("Condition=false: i={0}, j={1}", i, j);
            }

            Console.Read();
        }
    }
}

Output

Condition=false: i=1, j=1
Condition=false: i=1, j=0
  • Line 11: This is a If-Else statement and we're using logical AND & to evaluate the condition. ++i returns 1 so ++i > 1 is false. In this case, the whole condition will be false. But because we're using &, the logical expression following & must be evaluated. ++j returns 1 and ++j > 1 is false, too.
  • Line 14: The condition is false, so we'll run this statement to output i=1 and j=1.
  • Line 17-18: Reset i=0 and j=0 again.
  • Line 19: This time we're using && to evaluate the condition. ++i returns 1 so ++i > 1 is false. Because false && X is always returned false. The expression behind && will not be executed and we go to line 22 directly.
  • Line 14: Output i=1 and j=0 because ++j>0 has never been executed.
  • So you can see, you got different results by using && and & in the same code.

Example 01-29-02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;

namespace TestLogicalOr
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0, j = 0;

            if( ++i > 0 | ++j > 0  ){
                Console.WriteLine("Condition=true: i={0}, j={1}", i, j);
            }else{
                Console.WriteLine("Condition=false: i={0}, j={1}", i, j);
            }

            i = 0;
            j = 0;
            if( ++i > 0 || ++j > 0  ){
                Console.WriteLine("Condition=true: i={0}, j={1}", i, j);
            }else{
                Console.WriteLine("Condition=false: i={0}, j={1}", i, j);
            }

            Console.Read();
        }
    }
}

Output

Condition=true: i=1, j=1
Condition=true: i=1, j=0
  • Line 11: This time the condition is using logical | to connect the two logical expressions. ++i > 0 amd ++i > 0 all evaluate true so the code in line 12 will be executed.
  • Line 12: Output i=1 and j=1.
  • Line 19: We're using conditional || in the condition. Like &&, ++i > 0 evaluate true so the whole condition returns true. In this case, the code behind || won't be run any more.
  • Line 20: Output i=1 and j=0 because ++j>0 has never be run.

Summary

  • In && expression, if first logical expression returns false, the second one don't need to be executed. Because the whole expression will return false.
  • In || expression, if first logical expression returns true, the second one don't need to be executed. Because the whole expression will return true.
  • So using && and || are more efficient than using & and |.