Nested if else statement in C
×


Nested if else statement in C

176

Nested if-else statements in C allow for the creation of complex decision-making structures.

They involve placing one if-else statement inside another.

Syntax for if-else statement in C:

if (condition1) {
    // Code block 1
    if (condition2) {
        // Code block 2
    } else {
        // Code block 3
    }
} else {
    // Code block 4
}

In this structure, if condition1 is true, it executes "Code block 1".

If condition2 is true within "Code block 1", it executes "Code block 2"; otherwise, it executes "Code block 3".

If condition1 is false, it executes "Code block 4".

Example:

// program for if-else statement in C
#include<stdio.h> 

int main() {
    int num1 = 10;
    int num2 = 20;

    if (num1 == 10) {
        if (num2 == 20) {
            printf("Both numbers are 10 and 20.\n");
        } else {
            printf("First one is 10, but 2nd is not 20.\n");
        }
    } else {
        printf("First number is not 10.\n");
    }

    return 0;
}

Output:

Both numbers are 10 and 20.

In this program:

If num1 is 10, it checks if num2 is 20.

If both conditions are true, it prints "Both numbers are 10 and 20."

If the first condition is true but the second condition isfalse, it prints "First number is 10, but second number is not 20.

If the first condition is false, it prints "First number is not 10."



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments