Home » Blog » Nested if…else Statement In C

Nested if…else Statement In C

Decision making with if statement may be implemented in different forms depending on the complexity of conditions to be tested.The different forms are,

  1. Simple if statement
  2. if….else statement
  3. Nested if….else statement
  4. Using else if statement

Syntax

if( expression )
{
    if( expression1 )
    {
        statement block1;
    }
    else 
    {
        statement block2;
    }
}
else
{
    statement block3;
}

If the expression is false then block3 will be executed, and if expression is true then again expression1 is evaluated, if it is true then block1 is executed, and if false then block2 will be executed.

Example is shown below ::

#include<stdio.h>
#include<conio.h>

void main( )
{
    int a, b, c;
	
    clrscr();
	
    printf("Enter 3 numbers...");
    scanf("%d%d%d",&a, &b, &c);
	
    if(a > b)
    { 
        if(a > c)
        {
            printf("a is the greatest");
        }
        else 
        {
            printf("c is the greatest");
        }
    }
    else
    {
        if(b > c)
        {
            printf("b is the greatest");
        }
        else
        {
            printf("c is the greatest");
        }
    }
	
    getch();
}

OutPut

Enter 3 numbers... 2 12 4
	
b is the greatest

It’s Done. Keep It Up Guys…

Leave a Reply

Your email address will not be published.