Relational Operators

We make comparisons all the time, and so do programs. A program may need to determine whether one value is equal to, greater than, or less than another value. For example, if a program calculates the cost of a ticket to a movie in which children less than 12 get in free, it needs to find out if the customer s age is less than 12.

Programs compare values by using a relational operator. Table 5-1 lists the relational operators supported by C++:

Table 5-1: Relational Operators

Operator

Meaning

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

==

Equal to

!=

Not equal to

Relational Expressions

Like the arithmetic operators discussed in the last chapter, the relational operators are binary ”that is, they compare two operands. A statement with two operands and a relational operator between them is called a relational expression .

The result of a relational expression is a Boolean value, depicted as either true or false. Table 5-2 lists several relational expressions, using different relational operators and their values.

Table 5-2: Relational Expressions and Their Values

Relational Expression

Value

4 == 4

true

4 < 4

false

4 <= 4

true

4 > 4

false

4 != 4

false

4 == 5

false

4 < 5

true

4 <= 5

true

4 >= 5

false

4 != 5

true

Table 5-2 uses operands that have literal values. A literal value is a value that cannot change. 4 is a literal value, and cannot have a value other than the number 4.

Operands may also be variables (which were discussed in Chapter 3). The following program outputs the results of several variable comparisons.

` #include <iostream> using namespace std; int main(void) {  int a = 4, b = 5;  cout << a << " > " << b << " is " << (a > b) << endl;  cout << a << " >= " << b << " is " << (a >= b) << endl;  cout << a << " == " << b << " is " << (a == b) << endl;  cout << a << " <= " << b << " is " << (a <= b) << endl;  cout << a << " < " << b << " is " << (a < b) << endl;  return 0; } `

The program s output is

` 4 > 5 is 0 4 >= 5 is 0 4 == 5 is 0 4 <= 5 is 1 4 < 5 is 1 `

In the output, 0 is false and 1 is true. 0 is the integer value of Boolean false, while 1 is the usual integer value of Boolean true. As you may recall from Chapter 1, early computers consisted of wires and switches in which the electrical current followed a path that depended on which switches were in the on position (corresponding to the value one) or the off position (corresponding to the value zero). The on position corresponds to Boolean true, the off position to Boolean false.

 Caution While the usual integer value of logical true is 1, any non-zero number may be logical true. Therefore, in a Boolean comparison, do not compare a value to 1, compare it to true.

The data types of the two operands need not be the same. For example, you could change the data type of the variable b in the preceding program from an int to a float and the program still would compile and provide the same output. However, the data types of the two operands need to be compatible. As you may recall from Chapter 3, compatibility means, generally , that if one of the variable operands in the relational expression is a numeric data type, then the expression s other variable operand must also be a numeric data type.

For example, the program would not compile if you changed the data type of the variable b in the preceding program from an int to a string.

Precedence

Relational operators have higher precedence than assignment operators and lower precedence than arithmetic operators. Table 5-3 lists precedence among relational operators.

Table 5-3: Precedence of Relational Operators

Precedence

Operator

Highest

> >= < <=

Lowest

== !=

Operators in the same row have equal precedence. The associativity of relational operators of equal precedence is from left to right. C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148