-->

Nested If Statement

Nested if Statement
Placing If Statement inside another IF Statement is called Nested If in C Programming.
Syntax 

 if(expression 1)
   {
        if( expression 2)
       {   
           statement 1;
         }
      else
        { 
         statement2;
          }
      }
else {
            statement 3; 
          }
Statement one is executed only when both expression are true.
Statement two is executed only when expression one is true and expression two is false
Statement three is executed only when expression one are false.
Flow chart




Example 1: C program to find Largest of three number using nested if .

   #include<stdio.h>
  void main( )
  {
     int a,b,c;
    printf(" Enter the for A ,B,C to find largest among three \n");
    scanf(" %d %d %d" , &a,&b,&c);
   
    if(a>b)
       {
           if( a>c)   // inner nested if statement 
            {
                 printf(" A is Larger than B & C \n");
              }
            else 
             {
                 printf(" C is Larger than A & B \n");
               }
          }// end of outer if statement 
      else if ( b>c)
         {
            printf(" B is Larger than A & C \n");
           }
        else 
        {
            printf(" C is Larger than A & B \n");
          }
}// End of main function 

1 comment:

sunset said...

c computer programming samples
Egg Game in C

Post a Comment

Basic structure of c program