Previous Table of Contents Next


Relational Operators

The relational operators are used to determine whether two numbers are equal, or if one is greater or less than the other. Every relational expression returns either 1 (true) or 0 (false). The relational operators are presented in Table 4.1.

If the integer variable myAge has the value 39, and the integer variable yourAge has the value 40, you can determine whether they are equal by using the relational “equals” operator:

myAge == yourAge; // is the value in myAge the same as in yourAge?

This expression evaluates to 0, or false, because the variables are not equal. The expression

myAge > yourAge; // is myAge greater than yourAge?

evaluates to 0 or false.

Many novice C++ programmers confuse the assignment operator (=) with the equals operator (==). This can create a nasty bug in your program

Relational Operators

The relational operators are used to determine whether two numbers are equal, or if one is greater or less than the other. Every relational expression returns either 1 (true) or 0 (false). The relational operators are presented in Table 4.1.

If the integer variable myAge has the value 39, and the integer variable yourAge has the value 40, you can determine whether they are equal by using the relational “equals” operator:

myAge == yourAge; // is the value in myAge the same as in yourAge?

This expression evaluates to 0, or false, because the variables are not equal. The expression

myAge > yourAge; // is myAge greater than yourAge?

evaluates to 0 or false.

There are six relational operators: equals (==), less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), and not equals (!=). Table 4.1 shows each relational operator, its use, and a sample code use.

Table 4.1 The Relational Operators

Name Operator Sample Evaluates

Equals == 100 == 50; false
50 == 50; true
Not Equals != 100 != 50; true
50 != 50; false
Greater Than > 100 > 50; true
50 > 50; false
Greater Than >= 100 >= 50; true
or Equals 50 >=50; true
Less Than < 100 < 50; false
50 < 50; false
Less Than <= 100 <= 50; false
or Equals 50 <= 50; true

The if Statement

Normally, your program flows along line by line in the order in which it appears in your source code. The if statement enables you to test for a condition (such as whether two variables are equal) and branch to different parts of your code depending on the result.

The simplest form of an if statement is this:

if (expression)
 statement;

The expression in the parentheses can be any expression at all, but it usually contains one of the relational expressions. If the expression has the value zero, it is considered false, and the statement is skipped. If it has any nonzero value, it is considered true, and the statement is executed. Look at this example:

if (bigNumber > smallNumber)
 bigNumber = smallNumber;

This code compares bigNumber and smallNumber. If bigNumber is larger, the second line sets its value to the value of smallNumber.

The else Clause

Often your program will want to take one branch if your condition is true, another if it is false.

The method shown so far, testing first one condition and then the other, works fine but is a bit cumbersome. The keyword else can make for far more readable code:

if (expression)
 statement;
else
 statement;

Listing 4.3 demonstrates the use of the keyword else.

Listing 4.3 Demonstrating the else Keyword


1:   // Listing 4.3 - demonstrates if statement
2:   // with else clause
3:   #include <iostream.h>
4:   int main()
5:   {
6:     int firstNumber, secondNumber;
7:     cout << “Please enter a big number: “;
8:     cin >> firstNumber;
9:     cout << “\nPlease enter a smaller number: “;
10:     cin >> secondNumber;
11:     if (firstNumber > secondNumber)
12:         cout << “\nThanks!\n”;
13:     else
14:         cout << “\nOops. The second is bigger!”;
15:
16:     return 0;
17:  }


Previous Table of Contents Next