A relational operators compares two values. The values can be any built in C++ data type, such as char, int, and float, or as we’ll see later they can be user defined classes. The comparison involves such relationships as equal to, less than, and greater than. The result of the comparison is true or false; for example, either two values are equal (true), or they’re not (false).
Our first program, RELAT, demonstrates relational operators in a comparison of integer variables and constants.
// relat.cpp // demonstrates relational operators #include <iostream> using namespace std; int main() { int numb; cout << “Enter a number: “; cin >> numb; cout << “numb<10 is “ << (numb < 10) << endl; cout << “numb>10 is “ << (numb > 10) << endl; cout << “numb==10 is “ << (numb == 10) << endl; return 0; }
This program performs three kinds of comparisons between 10 and a number entered by the user. Here’s the output when the user enters 20:
Enter a number: 20 numb<10 is 0 numb>10 is 1 numb==10 is 0
The first expression is true if numb is less than 10. The second expression is true if numb is greater than 10, and the third is true if numb is equal to 10. As you can see from the output, the C++ compiler considers that a true expression has the value 1, while a false expression has the value 0.
As we mentioned in the last chapter, Standard C++ includes a type bool, which can hold one of two constant values, true or false. You might think that results of relational expressions like numb<10 would be of type bool, and that the program would print false instead of 0 and true instead of 1. In fact, C++ is rather schizophrenic on this point. Displaying the results of relational operations, or even the values of type bool variables, with cout<< yields 0 or 1, not false or true.
Historically this is because C++ started out with no bool type. Before the advent of Standard C++, the only way to express false and true was with 0 and 1. Now false can be represented by either a bool value of false, or by an integer value of 0; and true can be represented by either a bool value of true or an integer value of 1.
In most simple situations the difference isn’t apparent because we don’t need to display true/false values; we just use them in loops and decisions to influence what the program will do next.
Here’s the complete list of C++ relational operators:
Operator Meaning ----------------------------------------- > Greater than (greater than) < Less than == Equal to != Not equal to >= Greater than or equal to <= Less than or equal to
Now let’s look at some expressions that use relational operators, and also look at the value of each expression. The first two lines are assignment statements that set the values of the variables harry and jane. You might want to hide the comments with your old Jose Canseco baseball card and see whether you can predict which expressions evaluate to true and which to false.
jane = 44; //assignment statement harry = 12; //assignment statement (jane == harry) //false (harry <= 12) //true (jane > harry) //true (jane >= 44) //true (harry != 12) // false (7 < harry) //true (0) //false (by definition) (44) //true (since it’s not 0)
Note that the equal operator, ==, uses two equal signs. A common mistake is to use a single equal sign the assignment operator as a relational operator. This is a nasty bug, since the compiler may not notice anything wrong. However, your program won’t do what you want (unless you’re very lucky).
Although C++ generates a 1 to indicate true, it assumes that any value other than 0 (such as –7 or 44) is true; only 0 is false. Thus, the last expression in the list is true.
Now let’s see how these operators are used in typical situations. We’ll examine loops first, then decisions.
Read More Topics |
Arrays Programming in ANSI C |
Variables in C |
Common Programming Error in C |
Function Declaration in C |