nested if-statement in C ?


An if or if else statement within another if or if else statement is called “nested if statement” when an action has to be performed based on many decisions involving various types of expressions and variables then this statement is used so it is called multi way decision statement.



Syntax :-

if(cond1) {
if(cond2) {
      [Block C]
    }
else {
     [Block D]
    }
else {
     [Block E]
   }

Note:

if the cond1 is evaluated to true, the next inner if statement is executed. The cond2 is checked for true or false. if it is evaluated to true, then the statement in block C are executed otherwise, the statements in BlockD are executed.If above cond1 is not true then statement in Block E is executed.



Q.) W.A.P to print the greater number using simple if-else statement.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int a , b, c;
 printf(“enter three number”);
 scanf(“%d%d%d”,&a,&b,&c);
 if(a>b) 
 {
 if(a>c)
 printf(“Max =%d”,a); 
 else
 printf(“Max = %d”,c);
 }
 else
 {
 if(b>c)
 printf(“Max =%d”,b);
 else
 printf(“Max=%d”,c);
 }
 }
Output
enter three numbers:3 4 5
Max= 5