Comparison Operators
Now that we have an understanding of Boolean algebra, it is important to note that inprogramming we rarely construct expressions comprised solely of Boolean variables like that
of Listing 5.1. Instead, we usually construct Boolean expressions that arise by comparing
variables of the other types. In RobotC there are 6 operators designed to compare values
and return Boolean values. They are: greater than or equal, greater than, equal, less than,
less than or equal, and not equal. Each has its own syntax summarized in Table 5.3
Be wary of the == operator! A common programming error is to use the assignment
operator, =, to compare values. This error is exacerbated by the fact that, because the
mistaken syntax actually makes sense to the compiler (a fact that we will discuss later), it
will not cause a compiler error.
Listing 5.2 shows how to use comparison operators. It shows the common task of testing
whether a variable lies inside a certain range. The expressions in parentheses evaluate to
either true or false depending on the values of the variables x, y, and z.
float x=5.2 , y=0.0 , z =10.0;
bool s;
s = (x >=y) && (x <=z);
Listing 5.2: An example of using comparison operators. The value of s at the end of the
snippet is true. This shows how a programmer would test if y x z.
It is interesting to note that the comparison operators also work on string and character
values using alphabetical order. When comparing two strings/characters, whichever comes
rst in the dictionary is the smaller of the two. String/character comparisons are case-
sensitive with the rule that capital letters are less than their corresponding lower-case letters.