Logical Operators

So far, our Boolean expressions have consisted of only simple conditions, such as balance <= 20000 in line 13 of Listing 8.4 or (choice == "A") in line 11 of Listing 8.5, with only one comparison operator. However, it is often beneficial to combine several simple conditions (sub-Boolean expressions) into one larger Boolean expression. Logical operators allow us to do exactly that.

C# contains three logical operators that are semantically equivalent to “and,” “or,” and “not” in our everyday language. The latter are used in Figure 8.12 as a general introduction to C#'s equivalents. Notice that each phrase could be part of a normal conversation, and that we intuitively understand their meaning.

Figure 8.12. Three logical “operators” ...

Get C# Primer Plus 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.