Comparison Operators and Boolean Expressions


You have already seen a few of the comparison operators in action. This section presents the six fundamental comparison operators found in C# and explains how they form Boolean expressions.

A comparison operator allows two data values to be compared and generates a result of type bool. If the comparison is true, it produces the bool value true, and it produces the value false if the comparison is untrue.

The six comparison operators, can be divided into two categories relational and equality. They are all listed in Table 8.1.

Table 8.1. The Six Fundamental Comparison Operators
Math Notation C# Syntax Name Example Category Explanation
> > Greater-than x > y Relational Returns true if x is greater than y; otherwise, returns false
>= Greater-than-or-equal-to x >= y Relational Returns true if x is greater than or equal to y; otherwise, returns false
< < Less-than x < y Relational Returns true if x is less than y, otherwise; returns false
<= Less-than-or-equal-to equal to x <= y Relational Returns true if x is less than or y; otherwise, returns false
= == Equal-to x == y Equality Returns true if x is equal to y; otherwise, returns false
!= Not-equal-to x != y Equality Returns true if x is not equal to y; otherwise, returns false

x in the examples is usually referred to as the left operand and y is called the right operand.

C# contains one comparison operator not discussed in this chapter called the is operator. This operator is directly related to object-oriented programming and, for this reason, is not discussed until Chapter 17.

All comparison operators return one of the bool operators (true and false). Because a Boolean expression is an expression that is evaluated to be either true or false, comparison operators form Boolean expressions. This relationship is formally displayed in Syntax Box 8.4. Notice that each comparison operator combines exactly two operands to form a Boolean expression. Relational operators form relational expressions, and equality operators form equality expression.

Syntax Box 8.4 Comparison Operators and Boolean Expressions

 Boolean_expression::= <Relational_expression>                   ::= <Equality_expression>                   ::= <bool_Value> <Relational_expression>::= <Operand> <Relational_operator>  graphics/ccc.gif<Operand> <Equality_expression>::= <Operand> <Equality_operator> <Operand> <Operand> ::= <Literal>              ::= <Numerical_variable_identifier>              ::= <Numerical_constant_identifier>              ::= <Numerical_expression>              ::= <Method_call> <Relational_operator> ::=  >                          ::=  >=                          ::=  <                          ::=  <= <Equality_operator> ::=  ==                        ::=  != <Comparison_operator> ::= <Relational_operator>                          ::= <Equality_operator> 


When a <Method_call> is part of the Boolean expression, the method that is called must have a return value.

Only the equality operators can be applied to values of type bool.

It is possible to store the result of a Boolean expression in a variable of type bool. For example

 01: bool isPositive; 02: decimal balance = 100; 03: isPositive = (balance > 0); 

assigns true to isPositive in line 3.

A Boolean expression can simply be a value of type bool (<bool_Value>) as indicated in Syntax Box 8.4. Consequently, it is possible to utilize a variable of type bool in an if statement as shown by continuing the code example:

 04: if (isPositive) 05:        WriteLine("The account has a positive balance"); 

According to Syntax Box 8.4, comparison operators can combine operands, which also comprise numerical expressions. Thus, the following comparison is valid:


But how is this expression evaluated? For an answer, we need to look at the precedence rules of the operators involved. According to Appendix B, "Operator Precedence" (located at this book's Web site at, the relational and equality operators all have lower precedence than any of the arithmetic operators. As a result, arithmetic operations are always performed before comparisons, leading to the following evaluation of our example:




Even though the Boolean expression

 10 + 30 < 5 * 4 

might accomplish exactly what we intended, a couple of parentheses

 (10 + 30) < (5 * 4) 

enhances the readability and remove any ambiguity from the reader's mind as to how the expression is evaluated.

When two values of different types are compared with a comparison operator, the compiler follows exactly the same rules of implicit conversion and type promotion as when two values are combined with an arithmetic binary operator. These rules were illustrated in Figure 7.6 of Chapter 7, "Types Part II: Operators, Enumerators and Strings."

The following are a few examples of invalid comparisons. You can locate the problem by tracing the flowchart of Figure 7.6, of Chapter 7.



C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: