By Stephen R. Davis

When encountering if, C++ first executes the logical expression contained within the parentheses. In the following code, the program evaluates the conditional expression “is m greater than n.”

if (m > n)   // if m is greater than n...
{
             // ...then do this stuff
}

If the expression is true, that is, if m truly is greater than n, then control passes to the first statement after the { and continues from there. If the logical expression is not true, control passes to the first statement after the }.

Comparison operators in C++

The table shows the different operators that can be used to compare values in logical expressions.

Binary operators have the format expr1 operator expr2.

The Comparison Operators
Operator Meaning
== equality; true if the expression on the left of the
‘==’ has the same value as the expression on the
right
!= inequality; opposite of equality
> greater than; true if the left-hand expression is greater than
the one on the right
< less than; true if the left-hand expression is less than the
one on the right
>= greater than or equal to; true if the left-hand expression is
greater than or equal to the one on the right
<= less than or equal to; true if the left-hand expression is less
than or equal to the one on the right

Don’t confuse the equality operator (==) with the assignment operator (=). This is a common mistake for beginners.

The following BranchDemo program shows how the operators are used:

// BranchDemo - demonstrate the if statement
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
    // enter operand1 and operand2
    int  nOperand1;
    int  nOperand2;
    cout << "Enter argument 1:";
    cin  >> nOperand1;
    cout << "Enter argument 2:";
    cin  >> nOperand2;
    // now print the results
    if (nOperand1 > nOperand2)
    {
        cout << "Argument 1 is greater than argument 2"
             << endl;
    }
    if (nOperand1 < nOperand2)
    {
        cout << "Argument 1 is less than argument 2"
             << endl;
    }
    if (nOperand1 == nOperand2)
    {
        cout << "Argument 1 is equal to argument 2"
             << endl;
    }
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    cin.get();
    return 0;
}

Program execution begins with main() as always. The program first declares two int variables cleverly named nOperand1 and nOperand2. It then prompts the user to “Enter argument 1”, which it reads into nOperand1. The process is repeated for nOperand2.

The program then executes a sequence of three comparisons. It first checks whether nOperand1 is less than nOperand2. If so, the program outputs the notification “Argument 1 is less than argument 2”. The second if statement displays a message if the two operands are equal in value. The final comparison is true if nOperand1 is greater than nOperand2.

The following shows a sample run of the BranchDemo program:

Enter argument 1:5
Enter argument 2:10
Argument 1 is less than argument 2
Press Enter to continue . . .

The figure shows the flow of control graphically for this particular run.

image0.jpg

The way the BranchDemo program is written, all three comparisons are performed every time. This is slightly wasteful since the three conditions are mutually exclusive. For example, nOperand1 > nOperand2 can’t possibly be true if nOperand1 < nOperand2 has already been found to be true.

Say “No” to “No braces”

Actually the braces are optional. Without braces, only the first expression after the if statement is conditional. However, it’s much too easy to make a mistake this way, as demonstrated in the following snippet:

// Can't have a negative age. If age is less than zero...
if (nAge < 0)
    cout << "Age can't be negative; using 0" << endl;
    nAge = 0;
// program continues

You may think that if nAge is less than 0, this program snippet outputs a message and resets nAge to zero. In fact, the program sets nAge to zero anyway, no matter what its original value. The preceding snippet is equivalent to the following:

// Can't have a negative age. If age is less than zero...
if (nAge < 0)
{
    cout << "Age can't be negative; using 0" << endl;
}
    nAge = 0;
// program continues

It’s clear from the comments and the indent that the programmer really meant the following:

// Can't have a negative age. If age is less than zero...
if (nAge < 0)
{
    cout << "Age can't be negative; using 0" << endl;
    nAge = 0;
}
// program continues

The C++ compiler can’t catch this type of mistake. It’s just safer if you always supply the braces.

C++ treats all white space the same. It ignores the alignment of expressions on the page.

Always use braces to enclose the statements after an if statement, even if there’s only one. You’ll generate a lot fewer errors that way.