Skip to main content

Write about nested if ....else statement


Nested if....else statement

The general form of a nested if...else statement is,


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


if expression is false then statement-block3 will be executed,
otherwise the execution continues and enters
inside the first if to perform the check for the next if block,
where if expression 1 is true the statement-block1 is executed otherwise statement-block2 is executed.


#include <stdio.h>

    void main( )
    {
        int a, b, c;
        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");
            }
        }
    } 



written by -