6.1. Relational Operators

Making a decision about anything involves comparing one thing against another. It's no different in a computer program. You learned in Chapter 3 that statements are built up from operators and operands. I used the math operators to illustrate how program statements are constructed from those basic building blocks. In this section I want to discuss another set of operators that you have at your disposal: the relational operators. The relational operators are presented in Table 6-1.

Table 6-1. Table 6-1
Relational OperatorRelational TestsExampleOutcome of Example
==Equal15 == 20False
!=Not equal15 != 20True
<Less than15 < 20True
<=Less than or equal to15 <= 20True
>Greater than15 > 20False
>=Greater than or equal to15 >= 20False

In Table 6-1, every example has one operand on the left-hand side of the relational operator (15) and a second operand on the right-hand side of the operator (20). Once again, two operands are associated with the use of a relational operator, so all relational operators are binary operators. Therefore, combining two operands with a relational operator results in a relational expression.

Note that each relational expression ultimately resolves to a logic True or logic False state. In C#, logic True and logic False are expressed as Boolean values. The result of a Boolean expression resolves to the C# keywords true or false. In Table 6-1, each example shown in the third column resolves to the state shown in the fourth column. In the table, ...

Get Beginning 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.