C++ Decision Making

Decision making statements come in two forms in C++, ‘if’ statements and ‘switch’ statements. Both allow a decision to be made between two or more options.

‘if’ Statements

In its most basic form, an ‘if’ statement executes a group of statements if an expression evaluates to true. Its basic syntax is as follows.

if (expression)
{
    // Statement(s) will execute if the expression is true.
}

The following example checks whether the values of two variables are the same and displays a message in the console if they are.

int a = 10, b = 10;
if (a == b)
{
    cout << "a is equal to b" << endl;
}

This can be extended to execute a statement or statements if the expression is false as follows.

if (expression)
{
    // Statement(s) will execute if the expression is true.
}
else
{
    // Statement(s) will execute if the expression is false.
}

This example adds an ‘else’ statement to the one above to output a message if ‘a’ and ‘b’ are not equal.

int a = 10, b = 10;
if (a == b)
{
    cout << "a is equal to b" << endl;
}
else
{
    cout << "a is not equal to b" << endl;
}

The ‘if’ statement can be further extended with the use of ‘else if’. Any number of ‘else if’ statements can be used to extend the decision making process.

if (expression)
{
    // Statement(s) will execute if the expression is true.
}
else if (expression)
{
    /* Statement(s) will execute if the first expression is false
    and the second expression is true. */
}
else
{
    // Statement(s) will execute if both the expressions are false.
}

Again, the above example, that compares the variables ‘a’ and ‘b’ can be extended to check if they are equal, then check if ‘a’ is greater than ‘b’ and if neither of the conditions are true, output a third message.

int a = 10, b = 10;
if (a == b)
{
    cout << "a is equal to b" << endl; 
} 
else if (a > b)
{
    cout << "a is greater than b" << endl;
}
else
{
    cout << "a is less than b" << endl;
}

‘if’ statements can also be nested one inside another.

if (expression)
{
    // Statement(s) will execute if the expression is true.
    if (expression)
    {
        // Statement(s) will execute if the expression is true.
    }
}

Below are some examples of ‘if’ statements using the logical operators discussed in the section on Operators.

bool a = true;
bool b = true;
bool c = false;
if (a && b)
{
    // Display a message if 'a' and 'b' are true.
    cout << "Condition 1 is true" << endl;
}
if (c || b)
{
    // Display a message if 'c' or 'b' are true.
    cout << "Condition 2 is true" << endl;
}
if (c && b)
{
    // Display a message if 'c' and 'b' are true.
    cout << "Condition 3 is true" << endl;
}
else
{
    // Display a message if 'c' and/or 'b' are not true.
    cout << "Condition 3 is not true" << endl;
}
if (!(c && b))
{
    // Display a message if 'c' and/or 'b' are not true.
    cout << "Condition 4 is true" << endl;
}

The results of running the above will be as follows.

Condition 1 is true
Condition 2 is true
Condition 3 is not true
Condition 4 is true

The Ternary Operator

The ternary operator provides a means to write an ‘if’ statement in shorthand form.

expression ? true : false

The expression can be the same as in a normal ‘if’ statement. If it evaluates to true then the code after the question mark gets executed, otherwise the code after the colon gets executed.

The following example produces the same results as the ‘if-else’ statement above. Here the ternary operator is used as part of a ‘cout’ statement, to incorporate ‘equal to’ or ‘not equal to’, into a message, depending on whether the variables are equal or not.

int a = 10, b = 10;
cout << "a is " << (a == b ? "equal to" : "not equal to") << " b" << endl;

‘switch’ Statements

Where there are more than two options in a decision, a ‘switch’ statement can be more efficient than using multiple ‘else if’ statements.

switch (expression) 
{
    case expression-result-option1 :
        // statement(s) to execute.
        break;
    case expression-result-option2 :
        // Statement(s) to execute.
        break;
    case expression-result-option3 :
        // Statement(s) to execute.
        break;
    default :
        // Statement(s) to execute.
        break;
}

A simple example of a ‘switch’ statement would be to display a message in the console depending on the value of a variable.

int a = 3;
switch (a) {
    case 1 :
        cout << "a equals 1" << endl;
        break;
    case 2 :
        cout << "a equals 2" << endl;
        break;
    case 3 :
        cout << "a equals 3" << endl;
        break;
    default :
       cout << "a is not equal to 1, 2 or 3" << endl;
       break;
}

A ‘switch’ statement can have any number of options. When the ‘break’ statement is reached, the flow of control jumps to the statement following the end of the ‘switch’. The final ‘default’ statement is a catch all if none of the other options evaluate to true and is optional.