C

Nested if....else statement

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

Syntax:

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

if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to perform the test for 'expression1' . If the 'expression1' is true the 'statement-block1' is executed otherwise 'statement-block2' is executed.

Example:

//Write a C Program which demonstrate use of nested if-else statement.
#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b,c;
    clrscr();
    printf("Enter 3 number to compare: ");
    scanf("%d%d%d",&a,&b,&c);
    if(a > b)
    {
        if(a > c)
        {
            printf("a is greatest");
        }
        else
        {
            printf("c is greatest");
        }
    }
    else
    {
        if(b > c)
        {
            printf("b is greatest");
        }
        else
        {
            printf("c is greatest");
        }
    }
    getch();
}

Output:

Command Prompt
Enter 3 Number to compare: 5
7
9
c is greatest



Subscribe us on Youtube

Share This Page on


Ask Question