HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 5 Expressions and Operators

Relational Operators (>, >=, <, ==, !=)

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Syntax

exp1 > exp2

Greater than.

exp1 >= exp2

Greater than or equal to.

exp1 < exp2

Less than.

exp1 <= exp2

Less than or equal to.

exp1 == exp2

Equal to.

exp1 != exp2

Not equal to.

Arguments

exp1

Any expression.

exp2

Any expression.

Description

A relational expression consists of two expressions separated by one of six relational operators. The relational expression evaluates either to 1 (true) or 0 (false).

The equality operator (==) performs the same function as Pascal's = or Fortran's .EQ.; it just looks different. Although the equality operator looks similar to the assignment operator (=), the two operators serve completely different purposes. Use the assignment operator when you want to assign a value to a variable, but use the equality operator when you want to test the value of an expression.

Confusing = with ==

One of the most common mistakes made by beginners and experts alike is to use the assignment operator (=) instead of the equality operator (==). For instance:

while (j = 5)
do_something();

What is intended, clearly, is that the do_something() function should only be invoked if j equals five. It should be written

while (j == 5)
do_something();

The first version is syntactically legal, since all expressions have a value. The value of the expression j = 5 is 5. Since this is a nonzero value, the while expression will always evaluate to true and do_something() will always be invoked.

Relational Operators Precedence Rules

Relational operators have lower precedence than arithmetic operators. The expression

a + b * c < d / f

is evaluated as if it had been written

(a + (b * c)) < (d / f)

Among the relational operators, >, >=, <, and <= have the same precedence. The == and != operators have lower precedence. All of the relational operators have left-to-right associativity. The following table illustrates how the compiler parses complex relational expressions.

Table 5-12 Examples of Expressions Using the Relational Operators

Given the following declaration:

int j = 0, m = 1, n = -1;
float x = 2.5, y = 0.0;

Expression

Equivalent Expressions

Result

j > m

j > m

0

m / n < x

(m / n) < x

1

j <= m >= n

((j <=m) >= n)

1

j <= x == m

((j <= x) == m)

1

- x + j == y > n > m

((-x) + j) == ((y > n) >= m)

0

x += (y >= n)

x = (x + (y >= n))

3.5

++j == m != y * 2

((++j) == m) != (y * 2)

1

 

Evaluation of Relational Expressions

Relational expressions are often called Boolean expressions, in recognition of the nineteenth-century mathematician and logician, George Boole. Many programming languages, such as Pascal, have Boolean data types for representing true and false. The C language, however, represents these values with integers. Zero is equivalent to false, and any nonzero value is considered true.

The value of a relational expression is an integer, either 1 (indicating the expression is true) or 0 (indicating the expression is false). The examples in the following table illustrate how relational expressions are evaluated:

Table 5-13 Relational Expressions

Expression

Value

-1 < 0

1

0 > 1

0

5 == 5

1

7 != -3

1

1 >= -1

1

1 > 10

0

 

Because Boolean values are represented as integers, you can write

if (j)
statement;

If j is any nonzero value, statement is executed; if j equals 0, statement is skipped. Likewise, the statement

if (isalpha(ch))

is exactly the same as

if (isalpha(ch) != 0)

The practice of using a function call as a Boolean expression is a common idiom in C. It is especially effective for functions that return 0 if an error occurs, since you can use a construct such as

if (func())
proceed;
else
error handler;

Dangers of Comparing Floating-Point Values

You may get unexpected results if you compare floating-point values for equality because floating-point representations are inexact for some numbers. For example, the following expression, though algebraically true, will evaluate to false on many computers:

(1.0/3.0 + 1.0/3.0 + 1.0/3.0) == 1.0

This evaluates to 0 (false) because the fraction 1.0/3.0 contains an infinite number of decimal places (3.33333...). The computer is only capable of holding a limited number of decimal places, so it rounds each occurrence of 1/3. As a result, the left side of the expression does not equal exactly 1.0.

This problem can occur in even more subtle ways. Consider the following code:

double divide(double num, double denom)
{
return num/denom;
}
int main(void)
{
double c, a = 1.0, b = 3.0;
c = a/b;
if (c != divide(a, b))
printf("Fuzzy doubles\n");
}

Surprisingly, the value stored in c may not equal the value returned by divide(). This anomaly occurs due to the fact that some computers can represent more decimal places for values stored in registers than for values stored in memory. Because the value returned by divide() is never stored in memory, it may not be equal to the value c, which has been rounded for memory storage.

NOTE: To avoid bugs caused by inexact floating-point representations, you should refrain from using strict equality comparisons with floating-point types.

Example

/*  Program name is "relational_example". This program
* does some mathematical calculations and shows
* C's relational operators in action.
*/
#include <stdio.h>

int main(void)
{
int num, i;
printf("\n");
num = 5;
printf("The number is: %d\n", num);
for (i = 0; i <= 2; i++)
{
if (num < 25)
{
num *= num;
printf("The number squared is: %d\n", num);
}
else if (num == 25) {
num *= 2;
printf("Then, when you double that, you get: %d\n", num);
}
else if (num > 25)
{
num -= 45;
printf("And when you subtract 45, you're back where ");
printf("you started at: %d\n", num);
} /* end if */
} /* end for */

if (num != 5)
printf("The programmer made an error in setting up this \
example\n");
}

If you execute this program, you get the following output:

The number is: 5
The number squared is: 25
Then, when you double that, you get: 50
And when you subtract 45, you're back where you started at: 5

© Hewlett-Packard Development Company, L.P.