Skip to content
“`html Else If, Else-If Ladder and Nested If in C

Else If, Else-If Ladder and Nested If in C

In C programming, decision-making statements are used when we want the program to execute different blocks of code depending on a condition.

if else nested if else-if ladder

1. Else If / Nested If

Sometimes, we need to check another condition only when the first condition is false. This can be achieved using an else block containing another if statement.

Important: An if statement placed inside another if or else block is called a nested if.

Basic Structure

if (condition1)
{
    // statements
}
else
{
    if (condition2)
    {
        // statements
    }
}

Example: Check Whether a Number is a Natural Number and Even

#include <stdio.h>

void main()
{
    int num = 13;

    if (num <= 0)
    {
        printf("%d is not a natural number", num);
    }
    else
    {
        if (num % 2 == 0)
        {
            printf("%d is even natural number", num);
        }
    }

    printf("\nRest of the program");
}
How it works:
  1. The program first checks whether num <= 0.
  2. If true, it prints that the number is not a natural number.
  3. If false, the else block is executed.
  4. Inside the else block, another if checks whether the number is even.

For num = 13, the first condition is false and the second condition 13 % 2 == 0 is also false. Therefore, only:

Rest of the program

2. Else-If Ladder

When multiple conditions need to be checked one after another, we can use an else-if ladder.

If one condition is true, its corresponding block is executed and the remaining conditions are skipped.

General Structure

if (condition1)
{
    // statement 1
}
else if (condition2)
{
    // statement 2
}
else if (condition3)
{
    // statement 3
}
else
{
    // statement when all conditions are false
}
Remember: The conditions in an else-if ladder are checked from top to bottom. As soon as a true condition is found, its block is executed and the remaining conditions are not checked.

Example: Display Number in Words

#include <stdio.h>

void main()
{
    int x;

    printf("Enter any number 2, 4, 6 or 8 only: ");
    scanf("%d", &x);

    if (x == 2)
    {
        printf("TWO");
    }
    else
    {
        if (x == 4)
        {
            printf("FOUR");
        }
        else
        {
            if (x == 6)
            {
                printf("SIX");
            }
            else
            {
                if (x == 8)
                {
                    printf("EIGHT");
                }
                else
                {
                    printf("Invalid Input");
                }
            }
        }
    }

    printf("\nRest of the program");
}

Same Logic Using an Else-If Ladder

#include <stdio.h>

void main()
{
    int x;

    printf("Enter any number 2, 4, 6 or 8 only: ");
    scanf("%d", &x);

    if (x == 2)
    {
        printf("TWO");
    }
    else if (x == 4)
    {
        printf("FOUR");
    }
    else if (x == 6)
    {
        printf("SIX");
    }
    else if (x == 8)
    {
        printf("EIGHT");
    }
    else
    {
        printf("Invalid Input");
    }

    printf("\nRest of the program");
}
Why use an else-if ladder? It is usually easier to read and maintain when there are several alternative conditions.

3. Nested If vs Else-If Ladder

Nested If Else-If Ladder
An if statement is placed inside another block. Multiple conditions are connected using else if.
Useful when the second condition depends on the first condition. Useful when there are multiple alternative conditions.
Can result in multiple levels of indentation. Usually easier to read.
Example: Check natural number first, then check whether it is even. Example: Check whether a number is 2, 4, 6, 8, or invalid.

4. Important Points Regarding Nested Blocks

  1. Close the innermost block first.
    For every opening curly brace {, there must be a corresponding closing curly brace }.
  2. Curly braces are optional for a single statement.
    If an if, else, or loop contains only one statement, curly braces can be omitted.
  3. C is a free-form language.
    Extra spaces, tabs, and indentation generally do not affect how C understands the program. However, proper indentation is strongly recommended because it makes the program easier to read and debug.

Example of Optional Curly Braces

if (x > 0)
    printf("Positive");

The above is valid because the if contains only one statement.

However, when multiple statements are required, curly braces should be used:

if (x > 0)
{
    printf("Positive");
    printf("\nNumber is greater than zero");
}
Best practice: Even when braces are optional, using curly braces consistently makes nested programs much easier to understand and prevents accidental mistakes.

5. Practice Questions

Solve the following programs using only Nested If and Else-If Ladder.

Practice 1: Biggest of Three Numbers

Write a C program to determine the biggest out of three user-entered numbers.

Example:

Enter three numbers:
25
10
18

Biggest number = 25

Hint: Use comparisons between the three numbers. Try solving the problem first using nested if and then using an else-if ladder.

Practice 2: Vowel or Consonant

Write a C program to accept any English alphabet from the user and display whether it is a vowel or consonant.

The user can enter both uppercase and lowercase alphabets.

Vowels are: A, E, I, O, U and a, e, i, o, u.

Example 1:

Enter an alphabet: E
E is a vowel

Example 2:

Enter an alphabet: k
k is a consonant

Example 3:

Enter an alphabet: 5
Invalid input
Challenge: First try solving this using a nested if. Then solve it again using an else-if ladder.

Additional Practice

  1. Write a C program to check whether a number is positive, negative, or zero using an else-if ladder.
  2. Write a C program to accept a number from 1 to 7 and display the corresponding day of the week.
  3. Write a C program to accept a student’s marks and display the grade using an else-if ladder.
  4. Write a C program to check whether a given year is a leap year using nested if.
  5. Write a C program to find the smallest among three numbers using nested if.

6. Quick Revision

  • Nested if: An if statement inside another if/else block.
  • else-if ladder: Used to check multiple conditions one after another.
  • Conditions are generally checked from top to bottom.
  • Once a condition in an else-if ladder becomes true, the remaining conditions are skipped.
  • Every opening { must have a matching closing }.
  • Curly braces can be omitted for a single statement, but using them consistently is recommended.
  • Proper indentation does not change the meaning of C code, but it greatly improves readability.
Remember:
Nested if is useful when one decision depends on another decision, while an else-if ladder is useful when choosing between several alternative conditions.
“`