What is Nested if-else Statement in C Language ??? with Free Notes

What is Nested if-else Statement ???

Nested if-else Statements means if-else Statement inside the main if-else statement. This types of Statements helps to work with multiple conditions. Let’s understand about it’s structure by seen the syntax of the Nested if-else Statement.

Syntax,

Here is the syntax of the Nested if-else Statement in C Language.

if (condition1) {
    // code block to execute if condition1 is true
    if (condition2) {
        // code block to execute if both condition1 and condition2 are true
    } else {
        // code block to execute if condition1 is true but condition2 is false
    }
} else {
    // code block to execute if condition1 is false
}

Here, “condition1” and “condition2” are expressions that evaluates to either true or false. Now Let’s understand this Nested if-else concepts briefly with the example,

C Language Notes

Example:

Here we take an example for Nested if-else Statement,

#include <stdio.h>

int main() {
    int age = 18;

    // Nested if-else statement example
    if (age >= 18) {
        if (age == 18) {
            printf("You just turned 18!\n");
        } else {
            printf("You are an adult.\n");
        }
    } else {
        printf("You are a minor.\n");
    }

    return 0;
}

Explanation about Example:

In this Nested if-else Statement, we are going to check the “age” variables is an adult or not. Like if the condition is true then it will check two conditions inside that and shows the output if it will correct otherwise we set else condition for false then it will show the output in false (You are Minor).

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top