Logical Operators and Conditionals

As you program, you’ll often want to test whether a condition is true; for example, using the if statement, which you’ll see in the next chapter. C# provides a set of logical operators for this: and, or, and not. These operators work with the Boolean variables introduced in Chapter 3. Remember that a bool variable can hold only one of two values: true or false. Boolean expressions can be a bit confusing if you’re not used to them, but they’re critical to controlling the flow of your program, as you’ll learn in Chapter 5. Let’s take this slowly, with some examples. Start with three bool variables:

bool p = true;
bool q = false;
book r = true;

The variable p, by itself, evaluates to true, as does r, and q by itself evaluates to false. Easy enough. The and operator, which uses the symbol &&, evaluates to true only if both variables are true:

p && q   // evaluates false
p && r   // evaluates true

The or operator, which uses the symbol ||, evaluates to true if either variable is true:

p || q   // evaluates true

The only way for an or expression to evaluate to false is if both variables are false. Of course, if both variables are true, the expression still evaluates to true.

The not operator is slightly different; it operates on only a single variable, and evaluates to the opposite of the value of the variable:

!p   // evaluates false
!q   // evaluates true

Tip

You may have noticed that the and and or operators use doubled symbols (&& and ||) instead of single ones (& and |). The single symbols are for logical or bitwise operations, which you don’t need to bother with in this chapter.

Let’s make things a bit more complicated. Often you will want to test whether two conditions are true, whether only one is true, or whether neither is true. An individual expression is enclosed in parentheses, and that expression is evaluated before anything else. Table 4-2 shows some more examples. The examples in this table assume two variables, x and y, in which x has the value 5 and y has the value 7.

Table 4-2. Logical operators

Name

Operator

Given this statement

The expression evaluates to

Logic

And

&&

(x == 3) && (y == 7)

False

Both must be true.

Or

||

(x == 3) || (y == 7)

True

Either or both must be true.

Not

!

!(x == 3)

True

Expression must be false.

The first line in Table 4-2 uses the and operator:

(x == 3) && (y == 7)

The entire expression evaluates false because one side (x == 3) is false. (Remember that x has the value 5 and y has the value 7.)

With the or operator, only one side must be true; the expression is false only if both sides are false. So, in the case of the example in Table 4-2:

(x == 3) || (y == 7)

the entire expression evaluates true because one side (y == 7) is true.

With a not operator, the statement is true if the expression is false, and vice versa. So, in the accompanying example:

!(x == 3)

the entire expression is true because the tested expression (x == 3) is false. (The logic is: “it is true that it is not true that x is equal to 3.”)

Boolean logic takes a little time and practice to get used to before you can “read” these expressions naturally, but with a little experience it becomes second nature. You’ll be seeing them a lot, starting in the next chapter. Conditional operators are what allow your program to take actions in response to certain data values. Without them, your program could run in only a straight line, from start to finish.

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.