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)
{
    printf("a is equal to b");
}

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)
{
    printf("a is equal to b");
}
else
{
    printf("a is not equal to b");
}

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)
{
    printf("a is equal to b");
}
else if (a > b)
{
    printf("a is greater than b");
}
else
{
    printf("a is less than b");
}

‘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.
    printf("Condition 1 is true\n");
}
if (c || b)
{
    // Display a message if 'c' or 'b' are true.
    printf("Condition 2 is true\n");
}
if (c && b)
{
    // Display a message if 'c' and 'b' are true.
    printf("Condition 3 is true\n");
}
else
{
    // Display a message if 'c' and/or 'b' are not true.
    printf("Condition 3 is not true\n");
}
if (!(c && b))
{
    // Display a message if 'c' and/or 'b' are not true.
    printf("Condition 4 is true\n");
}

The ‘\n’, within the ‘printf’ statements, is an escape sequence, which forces a new line at the end of the message. 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

‘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 :
        printf("a equals 1");
        break;
    case 2 :
        printf("a equals 2");
        break;
    case 3 :
        printf("a equals 3");
        break;
    default :
       printf("a is not equal to 1, 2 or 3");
       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.