Saturday 22 December 2018

Nested if Statement


Nested if Statement :
                                When an if statement occurs within another if statement, such type of if statement is called “ nested if “ statement. The flowchart and the syntax of nested-if statement is shown below:


Syntax :                               
if(condition-1)
                                                {
                                                     if(condition-2)
                                                     {
                                                                True statement-1;
                                                     }
                                                     else
                                                    {                                                            
                                                                False statement-1;
                                                      }
                                                }
                                                else
                                                {
                                                      if(condition-3)
                                                      {
                                                                True statemet-2;
                                                      }
                                                      else
                                                      {
                                                                False statement-2;
                                                      }
                                                }
                                                statement-3;                    
                In this first the condition-1 will be checked. If the condition is true, then further condition-2 will be checked. If condition-2 is true, then true statement-1 will be executed and after that statement-3 will be executed. But if the condition-2 is false, then false statement-1 will be executed and then statement-3 will be executed.
                If the condition-1 is false, then condition-3 will be checked. If condition-3 is true then the true statement-2 will be executed and after that statement-3 will be executed. But if the condition-3 is false then the false statement-2 will be executed and then statement-3 will be executed.
Ex:          #include<stdio.h>
                #include<conio.h>
                void main()
                {
                      int a,b,c,max;
                      clrscr();
                      printf(“Enter the values of A B and C:”);
                      scanf(“%d%d%d”,&a,&b,&c);
                      if(a>b)
                      {                                                                                                                                                                                
                          if(a>c)
                          {
                                max=a;
                          }
                        
                        else
                        {
                               max=c;
                        }
                    }
                    else
                    {
                         if(b>c)
                         {
                               max=b;
                         }
                         else
                         {
                                max=c;
                         }
                     }
                  printf(“\n Maximum value is %d”,max);
                  getch();
                }

No comments:

Post a Comment