C Programming Tutorial

C Programming Tutorial

Nested If Statement in C

Hello Everyone, In this article, we will cover the concept of nested if statement in C programming. Nested if statements allow you to evaluate multiple conditions within each other, providing a powerful way to implement complex decision-making logic. We will cover the basic syntax, usage, and practical examples to help you understand and effectively use nested if statements in your C programs. Whether you’re new to C or have some experience, this guide will help you to understand C If Else Statement effectively. Let’s get started.

A nested if statement is a programming construct that allows for multiple conditional statements to be evaluated sequentially, one inside the other.

The structure of a nested if statement involves an outer if statement, which contains an inner if statement. The inner if statement is only executed if the outer if statement is true. If the inner if statement is also true, then the code inside the inner if statement is executed.

Syntax of Nested if Statement

if condition1:
    # code to execute if condition1 is true
    if condition2:
        # code to execute if both condition1 and condition2 are true
    else:
        # code to execute if condition1 is true but condition2 is false
else:
    # code to execute if condition1 is false

In this syntax, the first if statement checks for a certain condition. If that condition is true, then the code block associated with that if statement is executed. However, if there is another if statement inside the first code block, then that nested if statement is evaluated only if the first condition is true.

If the nested if statement is also true, then the code block associated with it is executed. Otherwise, if the condition in the nested if statement is false, then the code block associated with the else statement of the nested if statement is executed.

If the first condition is false, then the code block associated with the else statement of the first if statement is executed.

C Program for Nested if Statement

#include <stdio.h>
int main() {
    // Define variables
    int x = 5;
    int y = 10;

    // Check if x is 5
    if (x == 5) {
        // If x is 5, check if y is 10
        if (y == 10) {
            printf("Both x and y are equal to 5 and 10, respectively.\\n");
        } else {
            printf("x is equal to 5, but y is not equal to 10.\\n");
        }
    } else {
        printf("x is not equal to 5.\\n");
    }

    return 0;
}

In this program, we define two variables x and y with values of 5 and 10, respectively. Then, we use a nested if statement to check if x is equal to 5. If x is equal to 5, then we check if y is equal to 10.

If both conditions are true (i.e., x is 5 and y is 10), then the code block associated with the nested if statement is executed, which prints the message “Both x and y are equal to 5 and 10, respectively.”.

If the first condition is true but the second condition is false (i.e., x is 5 but y is not 10), then the code block associated with the else statement of the nested if statement is executed, which prints the message “x is equal to 5, but y is not equal to 10.”.

If the first condition is false (i.e., x is not equal to 5), then the code block associated with the else statement of the outer if statement is executed, which prints the message “x is not equal to 5”. So depending on the values of x and y, this program will output one of three different Output.

Share This Article