Nested if-else statement in C

Nested IF ELSE statement in c programming; In this tutorial, you will learn how to use if else, nested if else statements in C programming with examples.

C Nested if else Statement

In c programming, A nested if statement is an if-else statement with another if statement as the if the body or the else body.

Syntax of C Nested if else Statement

The syntax of the nested if else statement in C programming; as shown below:

if(condition) {
    //Nested if else inside the body of "if"
    if(condition2) {
       //Statements inside the body of nested "if"
    }
    else {
       //Statements inside the body of nested "else"
    }
}
else {
    //Statements inside the body of "else"
}

Example 1 – Nested if…else

See the following c program for Nested if else statement; as shown below:

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    if (number1 >= number2) {
      if (number1 == number2) {
        printf("Result: %d = %d",number1,number2);
      }
      else {
        printf("Result: %d > %d", number1, number2);
      }
    }
    else {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}

Output of the above c program

Enter two integers: 10 5
Result: 10 > 5

Explaination of above c program

  • Take two input number from user in program.
  • Then the test expression using nested if else statement in c program.
  • Print result

Example 2 – C program for leap year using nested if elseusing if else statement

See the second c program for leap year using nested if else; as shown below:

#include <stdio.h>
int main()
{
    int y;

    printf("Enter year: ");
    scanf("%d",&y);

    if(y % 4 == 0)
    {
    	//Nested if else
        if( y % 100 == 0)
        {
            if ( y % 400 == 0)
                printf("%d is a Leap Year", y);
            else
                printf("%d is not a Leap Year", y);
        }
        else
            printf("%d is a Leap Year", y );
    }
    else
        printf("%d is not a Leap Year", y);

    return 0;
}

Output of the above c program

Enter year: 2022
2022 is not a Leap Year

Explanation of above c program

  • Take input year from user in c program.
  • Use nested if else statement to check whether a given year is leap or not
  • Print result.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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