If…else and if…else…if statement in C

In previous post, I explained to control programs flow of execution using simple if statement.

Simple if works as “if some condition is true then do some tasks”. It doesn’t specifies what to do if condition is false. A good program must think both ways. For example – if user inputs correct account number and pin, then allow money withdrawal otherwise show error message to user.

if...else statement is an extension of simple if. It works as “If some condition is true then, do some task otherwise do some other task”.

Syntax of if...else statement

if(boolean_expression)
{
    // Body of if
    // If expression is true then execute this
}
else
{
    // Body of else
    // If expression is false then execute this
}

In above syntax if the given Boolean expression is true then, execute body of if part otherwise execute body of else part. In any case either body if or body of else is executed. In no case both the blocks will execute.

Flowchart of if...else statement

If else statement flow chart

Example of if...else statement

Let us write program based on if...else statement. Write a program to input two numbers from user. Print maximum between both the given numbers.

/**
 * C program to find maximum between two numbers
 */

#include <stdio.h>

int main()
{
    /* Declare two integer variables */
    int num1, num2;

    /* Input two number from user */
    printf("Enter two numbers: ");
    scanf("%d%d", &num1, &num2);

    /* Compare both number using relational operator */
    if(num1 > num2)
    {
        /* If first number is greater than second */
        printf("First number is maximum.");
    }
    else
    {
        /* If first number is not greater than second */
        printf("Second number is maximum.");
    }

    return 0;
}

Output of the above program

Enter two numbers: 10
20
Second number is maximum.

Ladder if...else...if statement

Simple if statement gives ability to execute tasks based on some condition. Its extension if...else takes both sides of the condition and execute some statements if conditions is true or if the condition is false then, execute some other statement.

However, things in real life is not simple. Let us consider a real life conditional problem –

If I have at least $1500, then I will purchase Microsoft Surface Pro 4.
Else if I have at least $1200, then I will purchase Apple Mac book Air.
Else if I have at least $1000, then I will purchase HP ultra-book.
Else I will purchase some mid-level developer laptop.

In the above scenario each condition is dependent on parent condition and must be checked sequentially. Also if a condition is true then all other conditions must be ignored. These situations in C programming are handled using a combination of if...else...if statement.

if...else...if is an extension of if...else statement. It specifies “If some condition is true then execute some task; otherwise if some other condition is true, then execute some different task; if none conditions are true then execute some default task.”

Ladder if...else...if statement syntax

if (boolean_expression_1)
{
    // If expression 1 is true then execute 
    // this and skip other if 
}
else if (boolean_expression_2)
{
    // If expression 1 is false and 
    // expression 2 is true then execute 
    // this and skip other if
}
else if (boolean_expression_n)
{
    // If expression 1 is false, 
    // expression 2 is also false, 
    // expression n-1 is also false,
    // and expression n is true then execute 
    // this and skip else.
}
else
{
    // If no expressions are true then
    // execute this skipping all other.
}

Ladder if...else...if statement flowchart

Ladder if...else...if statement flow chart

Example of ladder if...else...if statement

Let us write a simple C program to input an integer from user. Check if the given integer is negative, zero or positive?

/**
 * C program to check negative, zero or positive.
 */

#include <stdio.h>

int main()
{
    /* Declare integer variable */
    int num;

    /* Input an integer from user */
    printf("Enter any number: ");
    scanf("%d", &num);

    if(num < 0)
    {
        /* If number is less than zero, then it is negative */
        printf("NUMBER IS NEGATIVE.");
    }
    else if(num == 0)
    {
        /* If number equal to 0, then it is zero */
        printf("NUMBER IS ZERO.");
    }
    else
    {
        /* If number is greater then zero, then it is positive */
        printf("NUMBER IS POSITIVE.");
    }
    return 0;
}

Output of the above program.

Enter any number: -22
NUMBER IS NEGATIVE.