C Programs Tutorials | IT Developer
IT Developer

C Programming - nested if statement in C



Share with a Friend

C Programming - nested if statement in C

Nested if Statement in C

A nested if statement in C is an if statement that appears inside another if or else block. It allows you to test multiple conditions in a hierarchical manner, where one condition is checked only if the outer condition is true (or false, depending on the logic).

Syntax of Nested if Statement:

C

if (condition1) {

    // Code to be executed if condition1 is true

    if (condition2) {

        // Code to be executed if condition1 and condition2 are true

    } else {

        // Code to be executed if condition1 is true and condition2 is false

    }

} else {

    // Code to be executed if condition1 is false

    if (condition3) {

        // Code to be executed if condition1 is false and condition3 is true

    }

}

  • The outer if condition is checked first.
  • If the outer condition is true, the inner if or else block will be checked.
  • Nested if statements allow more complex decision-making.

Example 1: Basic Nested if Statement

C

#include <stdio.h>

int main() {

    int a = 10, b = 5;

    if (a > 5) {  // Outer if

        if (b < 10) {  // Inner if

            printf("a is greater than 5 and b is less than 10\n");

        } else {

            printf("a is greater than 5 but b is not less than 10\n");

        }

    } else {

        printf("a is not greater than 5\n");

    }

    return 0;

}

Explanation:

  • The outer if checks if a > 5. Since a is 10, it is true.
  • Inside the outer if, there is another if that checks if b < 10. Since b is 5, it is true.
  • Therefore, the message "a is greater than 5 and b is less than 10" is printed.

Output:

a is greater than 5 and b is less than 10

Example 2: Nested if-else Statement

C

#include <stdio.h>

int main() {

    int a = 15, b = 10, c = 20;

    if (a > 10) {  // Outer if

        if (b > 5) {  // Inner if

            printf("Both a is greater than 10 and b is greater than 5\n");

        } else {

            printf("a is greater than 10 but b is not greater than 5\n");

        }

    } else {

        if (c < 25) {

            printf("a is not greater than 10, but c is less than 25\n");

        }

    }

    return 0;

}

Explanation:

  • The outer if checks if a > 10. Since a is 15, the condition is true.
  • Inside the outer if, it checks if b > 5. Since b is 10, it is true.
  • Therefore, "Both a is greater than 10 and b is greater than 5" is printed.

Output:

Both a is greater than 10 and b is greater than 5

Example 3: Nested if Statement with Multiple Conditions

C

#include <stdio.h>

int main() {

    int a = 20, b = 15, c = 10;

    if (a > b) {  // Outer if

        if (b > c) {  // Inner if

            printf("a is greater than b, and b is greater than c\n");

        } else {

            printf("a is greater than b, but b is not greater than c\n");

        }

    } else {

        printf("a is not greater than b\n");

    }

    return 0;

}

Explanation:

  • The outer if checks if a > b. Since a is 20 and b is 15, the condition is true.
  • Inside the outer if, it checks if b > c. Since b is 15 and c is 10, the condition is also true.
  • Thus, "a is greater than b, and b is greater than c" is printed.

Output:

a is greater than b, and b is greater than c

Example 4: Nested if-else with Multiple Levels

C

#include <stdio.h>

int main() {

    int a = 30, b = 25, c = 10;

    if (a > b) {  // Outer if

        if (b > c) {  // Inner if

            if (c < 15) {

                printf("a is greater than b, b is greater than c, and c is less than 15\n");

            } else {

                printf("a is greater than b, b is greater than c, and c is not less than 15\n");

            }

        } else {

            printf("a is greater than b, but b is not greater than c\n");

        }

    } else {

        printf("a is not greater than b\n");

    }

    return 0;

}

Explanation:

  • The outer if checks if a > b. Since a is 30 and b is 25, the condition is true.
  • The inner if checks if b > c. Since b is 25 and c is 10, it is true.
  • The innermost if checks if c < 15. Since c is 10, it is true, so the message "a is greater than b, b is greater than c, and c is less than 15" is printed.

Output:

a is greater than b, b is greater than c, and c is less than 15

Important Points to Remember:

  • Nesting Depth: if statements can be nested to any depth, allowing you to handle complex decision-making scenarios.
  • Logical Flow: A nested if block allows you to check further conditions only after a previous condition is true or false.
  • Clarity: While nested if statements are useful, they can become difficult to read if overused. It's best to structure code for readability.

Summary of Nested if Statements:

  • Nested if statements are if statements inside other if or else blocks.
  • They allow you to handle complex decision-making by testing multiple conditions in sequence.
  • It's essential to ensure the logical flow is clear and that the conditions are well-defined to avoid confusion.