站内搜索: 请输入搜索关键词
当前页面: 图书首页 > NET For Java Developers Migrating To C#

NET For Java Developers Migrating To C#

[ directory ] Previous Section Next Section

8.2 Logical Operators

Logical operators are used along with control flow statements to branch the execution of the program logic based on whether a condition is true or false. The &&, ||, and ! operators perform a logical AND, a logical OR, and a logical negation operation, respectively, on their operands. The && and || operators are binary operators and require two operands, which can be any two expressions that evaluate to true or false boolean values. The ! operator is the negation operator, and it negates the expression it is attached to. The true and false operators can be used for checking whether a particular condition is true or false. To simulate an infinite while loop, you can use the true operator:

while (true);

Listing 8.2 shows the C# logical operators. These have the same meaning in Java.

Listing 8.2 C# Logical Operators
using System;

public class LogicalOperators {

  public static void Main(string[] args) {

    int x = 4;
    int y = 2;
    Console.WriteLine("x = "+x+";y = "+y);
    if ((x == 4) && (y == 2)) {
      Console.WriteLine("Executing (x == 4) && (y == 2))");
    }
    if ((x == 4) || (y == 2)) {
      Console.WriteLine("Executing (x == 4) || (y == 2))");
    }
    if ((x == 4) || (y != 2)) {
      Console.WriteLine("Executing (x == 4) || (y != 2))");
    }
    if (!((y != 2) && (x != 4))) {
      Console.WriteLine("Executing !((y != 2) && (x != 4))");
    }
    if (!(y != 2) || (x == 4)) {
      Console.WriteLine("Executing !((y != 2) || (x == 4))");
    }
    if ((y != 2) && (x == 4)) {
      Console.WriteLine("Executing (y != 2) && (x == 4)");
    }

    if (true) {
      Console.WriteLine("Always printed");
    }

    if (false) {
      Console.WriteLine("Never printed");
    }

  }

}

The output of Listing 8.2 is as follows:

x = 4;y = 2
Executing (x == 4) && (y == 2))
Executing (x == 4) || (y == 2))
Executing (x == 4) || (y != 2))
Executing !((y != 2) && (x != 4))
Executing !((y != 2) || (x == 4))
Always printed

In Listing 8.2 the != and == operands get precedence over the && and || logical operators. (Operator precedence is discussed in Section 8.17.) For now, if you are familiar with Java it should be clear that the results are very much the same as you would expect in Java. Note that when evaluating the following line, the CLR uses a technique called short-circuiting (similar to the one in Java):

if ((y != 2) && (x == 4)) {
  Console.WriteLine("Executing (y != 2) && (x == 4)");
}

Basically, y != 2 evaluates to false; and because the LHS operand of a logical AND (&&) operator evaluates to false, there is no point in evaluating the RHS operand (a logical AND with false will always yield a false result). As you will see later, except for the true and false operators, the &&, ||, and ! operators cannot be overloaded.

    [ directory ] Previous Section Next Section