Nested if Statement in C

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 20 Feb, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read