Control Program Flow in C++ with Branch Commands

By Stephen R. Davis

The simplest form of flow control in C++ is the branch statement. This instruction allows the program to decide which of two paths to take through C++ instructions, based on the results of a logical expression.

In C++, the branch statement is implemented using the if statement:

if (m > n)
    // Path 1
    // ...instructions to be executed if
    // m is greater than n
    // Path 2
    // ...instructions to be executed if not

First, the logical expression m > n is evaluated. If the result of the expression is true, control passes down the path marked Path 1 in the previous snippet. If the expression is false, control passes to Path 2. The else clause is optional. If it is not present, C++ acts as if it is present but empty.

Actually, the braces are not required if there’s only one statement to execute as part of the if. Originally, braces were only used if there were two or more statements that you wanted to treat as one. However, people quickly realized that it was cleaner and less error prone if you used braces every time, no matter how many statements there are.

The following program demonstrates the if statement (note all the lovely braces):

// BranchDemo - input two numbers. Go down one path of the
//              program if the first argument is greater
//              than the first or the other path if not
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
    // input the first argument...
    int nArg1;
    cout << "Enter arg1: ";
    cin  >> nArg1;
   // ...and the second
    int nArg2;
    cout << "Enter arg2: ";
    cin  >> nArg2;
   // now decide what to do:
    if (nArg1 > nArg2)
        cout<< "Argument 1 is greater than argument 2"
            << endl;
        cout<< "Argument 1 is not greater than 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');
    return 0;

Here the program reads two integers from the keyboard and compares them. If nArg1 is greater than nArg2, control flows to the output statement cout << “Argument 1 is greater than argument 2”. If nArg1 is not greater than nArg2, control flows to the else clause where the statement cout << “Argument 1 is not greater than argument 2n” is executed. Here’s what that operation looks like:

Enter arg1: 5
Enter arg2: 6
Argument 1 is not greater than argument 2
Press Enter to continue...

Notice how the instructions within the if blocks are indented slightly. This is strictly for human consumption because C++ ignores whitespace (spaces, tabs, and newlines). It may seem trivial, but a clear coding style increases the readability of your C++ program.

The Code::Blocks editor can enforce this style or any one of several other coding styles for you. Select Settings→Editor, and then click on the Source Formatter selection from the scrolled list on the left.