Nested if-else statement in C-language - C-Tutorial

Latest

Saturday, 8 October 2016

Nested if-else statement in C-language

Nested if-else statement        An if-else statement is embedded within another if-else statement such representation is called as nested if-else statement.  The general format of nested if-else statement is:


           
              Syntax:           if(condition1)
                                    {
                                                if(condition2)
    {
     Statements-1 to be executed if condition2 is true
                                                }
                                                else
                                                {
                                                Statements-2 to be executed if condition2 is false
                                                }
                                    }
                                    else
                                    {
                                               Statements-3 to be executed if condition1 is false
                                    }

                                    Statements-X;


Ø  First condition1 is evaluated.  It produces either TRUE or FALSE.
Ø  If the condition1 is TRUE, then control enters into condition2 section. Condition2 is evaluated and produces either TRUE or FALSE.
o   If Condition2 is TRUE, then Statements-1 are executed and then control reaches to Statements-X. 
o   If Condition2 outcome is FALSE, then Statements-2 are executed and then control reaches to Statements-X.
Ø  If the condition1 outcome is FALSE, then Statements-3 are executed and then control reaches to Statements-X.

/* PROGRAM TO PRINT LARGEST NUMBER FROM THE GIVEN THREE NUMBERS */

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

main()
{
            int x,y,z;
            clrscr();
            printf("\nEnter Three Numbers:");
            scanf("%d%d%d",&x,&y,&z);
            if(x>y)
            {
                        if(x>z)
                                    printf("\nMaximum Number:%d",x);
                        else
                                    printf("\nMaximum Number:%d",z);
            }
            else
            {
                        if(y>=z)
                                    printf("\nMaximum Number:%d",y);
                        else
                                    printf("\nMaximum Number:%d",z);
            }
}

No comments:

Post a Comment