Decision Control Statements

In any programming language, there is a need to perform different tasks based on the condition. For example, consider an online website, when you enter wrong id or password it displays error page and when you enter correct credentials then it displays welcome page. So there must be a logic in place that checks the condition (id and password) and if the condition returns true it performs a task (displaying welcome page) else it performs a different task(displaying error page).

Using decision control statements we can control the flow of program in such a way so that it executes certain statements based on the outcome of a condition (i.e. true or false). In C Programming language we have following decision control statements.

  1. if statement
  2. if-else & else-if statement
  3. switch-case statements
  4. Nested if-else statement

Decision Control Statements in C

Here are the tutorial links:

  1. If statement: The statements inside if body executes only when the condition defined by if statement is true. If the condition is false then compiler skips the statement enclosed in if’s body. We can have any number of if statements in a C program.
  2. If-else statement: In this decision control statement, we have two block of statements. If condition results true then if block gets executed else statements inside else block executes. else cannot exist without if statement. In this tutorial, I have covered else-if statements as well.
  3. Switch-case statement: This is very useful when we need to evaluate multiple conditions. The switch block defines an expression (or condition) and case has a block of statements, based on the result of expression, corresponding case block gets executed. A switch can have any number of cases, however there should be only one default handler.

C if statement

if (testExpression) 
{
   // statements
}

The if statement evaluates the test expression inside the parenthesis.

If the test expression is evaluated to true (nonzero), statements inside the body of if is executed.

If the test expression is evaluated to false (0), statements inside the body of if is skipped from execution.

To learn more on when test expression is evaluated to nonzero (true) and 0 (false), check out relational and logical operators.


Flowchart of if statement

Flowchart of if statement in C programming

Example : C if statement

// Program to display a number if user enters negative number
// If user enters positive number, that number won't be displayed

#include <stdio.h>
int main()
{
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // Test expression is true if number is less than 0
    if (number < 0)
    {
        printf("You entered %d.\n", number);
    }

    printf("The if statement is easy.");

    return 0;
}

Output 1

Enter an integer: -2
You entered -2.
The if statement is easy.

When user enters -2, the test expression (number < 0) becomes true. Hence, You entered -2 is displayed on the screen.

Output 2

Enter an integer: 5
The if statement in C programming is easy.

When user enters 5, the test expression (number < 0) becomes false and the statement inside the body of if is skipped.

C if…else statement

The if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).


Syntax of if…else

if (testExpression) {
    // codes inside the body of if
}
else {
    // codes inside the body of else
}

If test expression is true, codes inside the body of if statement is executed and, codes inside the body of else statement is skipped.

If test expression is false, codes inside the body of else statement is executed and, codes inside the body of if statement is skipped.


Flowchart of if…else statement

Flowchart of if...else statement in C Programming


Example : C if…else statement

// Program to check whether an integer entered by the user is odd or even

#include <stdio.h>
int main()
{
    int number;
    printf("Enter an integer: ");
    scanf("%d",&number);

    // True if remainder is 0
    if( number%2 == 0 )
        printf("%d is an even integer.",number);
    else
        printf("%d is an odd integer.",number);
    return 0;
}

Output

Enter an integer: 7
7 is an odd integer.

When user enters 7, the test expression ( number%2 == 0 ) is evaluated to false. Hence, the statement inside the body of else statement printf("%d is an odd integer"); is executed and the statement inside the body of if is skipped.

Nested if…else statement (if…elseif….else Statement)

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.

The nested if…else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.


Syntax of nested if…else statement.

if (testExpression1) 
{
   // statements to be executed if testExpression1 is true
}
else if(testExpression2) 
{
   // statements to be executed if testExpression1 is false and testExpression2 is true
}
else if (testExpression 3) 
{
   // statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is true
}
.
.
else 
{
   // statements to be executed if all test expressions are false
}

Example #3: C Nested if…else statement

// Program to relate two integers using =, > or <

#include <stdio.h>
int main()
{
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    //checks if two integers are equal.
    if(number1 == number2)
    {
        printf("Result: %d = %d",number1,number2);
    }

    //checks if number1 is greater than number2.
    else if (number1 > number2)
    {
        printf("Result: %d > %d", number1, number2);
    }

    // if both test expression is false
    else
    {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}

Output

Enter two integers: 12
23
Result: 12 < 23

You can also use switch statement to make decision between multiple possibilities.

C switch…case Statement

The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.


Syntax of switch…case

switch (n)
​{
    case constant1:
        // code to be executed if n is equal to constant1;
        break;

    case constant2:
        // code to be executed if n is equal to constant2;
        break;
        .
        .
        .
    default:
        // code to be executed if n doesn't match any constant
}

When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.

In the above pseudocode, suppose the value of n is equal to constant2. The compiler will execute the block of code associate with the case statement until the end of switch block, or until the break statement is encountered.

The break statement is used to prevent the code running into the next case.


switch Statement Flowchart

Flowchart of switch statement

 

Example: switch Statement

// Program to create a simple calculator
// Performs addition, subtraction, multiplication or division depending the input from user

# include <stdio.h>

int main() {

    char operator;
    double firstNumber,secondNumber;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);

    printf("Enter two operands: ");
    scanf("%lf %lf",&firstNumber, &secondNumber);

    switch(operator)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
            break;

        case '-':
            printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
            break;

        case '*':
            printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
            break;

        case '/':
            printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
            break;

        // operator is doesn't match any case constant (+, -, *, /)
        default:
            printf("Error! operator is not correct");
    }

    return 0;
}

Output

Enter an operator (+, -, *,): -
Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1

The  operator entered by the user is stored in operator variable. And, two operands 32.5 and 12.4 are stored in variables firstNumber and secondNumber respectively.

Then, control of the program jumps to

printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);

Finally, the break statement ends the switch statement.

If break statement is not used, all cases after the correct case is executed.