Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

2.7 Decision Making: Equality and Relational Operators

We now introduce a simple version of C++'s if statement that allows a program to take alternative action based on whether a condition is true or false. If the condition is true, the statement in the body of the if statement is executed. If the condition is false, the body statement is not executed. We'll see an example shortly.

Conditions in if statements can be formed by using the equality operators and relational operators summarized in Fig. 2.12. The relational operators all have the same level of precedence and associate left to right. The equality operators both have the same level of precedence, which is lower than that of the relational operators, and associate left to right.

Fig 2.12. Equality and relational operators.

Standard algebraic equality or relational operator

C++ equality or relational operator

Sample C++ condition

Meaning of C++ condition

Relational operators

>

>

x > y

x is greater than y

<

<

x < y

x is less than y

>=

x >= y

x is greater than or equal to y

<=

x <= y

x is less than or equal to y

Equality operators

=

==

x == y

x is equal to y

!=

x != y

x is not equal to y

Using the if Statement

The following example (Fig. 2.13) uses six if statements to compare two numbers input by the user. If the condition in any of these if statements is satisfied, the output statement associated with that if statement is executed.

Fig 2.13. Comparing integers using if statements, relational operators and equality operators. (Part 1 of 2.)

 1  // Fig. 2.13: fig02_13.cpp
 2  // Comparing integers using if statements, relational operators
 3  // and equality operators.
 4  #include <iostream> // allows program to perform input and output
 5
 6  using std::cout; // program uses cout
 7  using std::cin; // program uses cin  
 8  using std::endl; // program uses endl
 9
10  // function main begins program execution
11  int main()
12  {
13     int number1; // first integer to compare
14     int number2; // second integer to compare
15

16     cout << "Enter two integers to compare: "; // prompt user for data
17     cin >> number1 >> number2; // read two integers from user
18

19     if ( number1 == number2 )                       
20        cout << number1 << " == " << number2 << endl;

21
22     if ( number1 != number2 )
23        cout << number1 << " != " << number2 << endl;
24
25     if ( number1 < number2 )
26        cout << number1 << " < " << number2 << endl;
27
28     if ( number1 > number2 )
29        cout << number1 << " > " << number2 << endl;
30
31     if ( number1 <= number2 )
32        cout << number1 << " <= " << number2 << endl;
33
34     if ( number1 >= number2 )
35        cout << number1 << " >= " << number2 << endl;
36  } // end function main

using Directives

Lines 6–8

using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl

are using directives that eliminate the need to repeat the std:: prefix as we did in earlier programs. We can now write cout instead of std::cout, cin instead of std::cin and endl instead of std::endl, respectively, in the remainder of the program.

In place of lines 6–8, many programmers prefer to use the directive

using namespace std;

which enables a program to use all the names in any standard C++ header (such as <iostream>) that a program might include. From this point forward in the book, we'll use the preceding directive in our programs.

Variable Declarations and Reading the Inputs from the User

Lines 13–14

int number1; // first integer to compare
int number2; // second integer to compare

declare the variables used in the program.

The program uses cascaded stream extraction operations (line 17) to input two integers. Remember that we're allowed to write cin (instead of std::cin) because of line 7. First a value is read into variable number1, then a value is read into variable number2.

Comparing Numbers

The if statement in lines 19–20

if ( number1 == number2 )
   cout << number1 << " == " << number2 << endl;

compares the values of variables number1 and number2 to test for equality. If the values are equal, the statement in line 20 displays a line of text indicating that the numbers are equal. If the conditions are true in one or more of the if statements starting in lines 22, 25, 28, 31 and 34, the corresponding body statement displays an appropriate line of text.

Each if statement in Fig. 2.13 has a single statement in its body and each body statement is indented. In Chapter 4 we show how to specify if statements with multiple-statement bodies (by enclosing the body statements in a pair of braces, { }, creating what's called a compound statement or a block).

White Space

Note the use of white space in Fig. 2.13. Recall that white-space characters, such as tabs, newlines and spaces, are normally ignored by the compiler. So, statements may be split over several lines and may be spaced according to your preferences. It's a syntax error to split identifiers, strings (such as "hello") and constants (such as the number 1000) over several lines.

Operator Precedence

Figure 2.14 shows the precedence and associativity of the operators introduced in this chapter. The operators are shown top to bottom in decreasing order of precedence. All these operators, with the exception of the assignment operator =, associate from left to right. Addition is left-associative, so an expression like x + y + z is evaluated as if it had been written (x + y) + z. The assignment operator = associates from right to left, so an expression such as x = y = 0 is evaluated as if it had been written x = (y = 0), which, as we'll soon see, first assigns 0 to y, then assigns the result of that assignment—0—to x.

Fig 2.14. Precedence and associativity of the operators discussed so far.

Operators

Associativity

Type

()

[See caution in Fig. 2.10]

grouping parentheses

*

/

%

left to right

multiplicative

+

-

left to right

additive

<<

>>

left to right

stream insertion/extraction

<

<=

>

>=

left to right

relational

==

!=

left to right

equality

=

right to left

assignment

  • + Share This
  • 🔖 Save To Your Account