Nested if Statement in C

By | February 20, 2023

Prerequisite – Control Statements in C
A nested “if” statement in C is a control statement that allows for the use of a secondary “if” statement within the body of another “if” statement. The nested “if” statement can be used to perform additional checks or to perform more specific tasks when a particular condition is met.

Syntax:
The syntax for a nested “if” statement is as follows:

if (condition1) {
    // code to be executed if condition1 is true
    if (condition2) {
        // code to be executed if both condition1 and condition2 are true
    }
}

In this example, the first “if” statement checks for condition1, and if that condition is true, the program enters the nested “if” statement and checks for condition2. If both conditions are true, the program executes the code inside the second set of braces.

Nested “if” statements can be nested further within other “if” statements, allowing for even more complex conditional logic. It is important to ensure that the nested statements are properly indented and that the code is structured in a way that is easy to read and understand.

Example:
Here’s an example of using nested “if” statements in C:

#include <stdio.h>

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num >= 0) {
        if (num == 0) {
            printf("You entered zero.\n");
        } else {
            printf("You entered a positive number.\n");
        }
    } else {
        printf("You entered a negative number.\n");
    }

    return 0;
}

In this example, the user enters a number, and the program checks if it’s positive, negative, or zero. If the number is greater than or equal to 0, the program enters the nested “if” statement and checks if the number is equal to 0. If the number is 0, the code inside the first set of braces is executed, which prints “You entered zero.” to the console. If the number is greater than 0, the code inside the second set of braces is executed, which prints “You entered a positive number.” to the console. If the number is less than 0, the code inside the “else” block is executed, which prints “You entered a negative number.” to the console.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.