C Nested else if statement

C Nested else if is concept of placing a conditional block in another conditional block.
C Nested else if statements can be applied for if and else part also.
C Nested else if statement can be applied upto 255 blocks.
When we are working with a nested if else at any given point of time one condition can be executed.

Syntax:

if(condition)
{
block1;
}
else
{
if(condition2)
{
block2;
}
else
{
if(condition3)
block3;
else
block4;
}
}
}

  • If condition 1 is true, then block1 will be executed. If it is false then contro, will be passed to else part block.
  • Within the lese part if condition 2 is true then block2 will be executed, if it is false then control will be nested else part block.
  • Within the nested else part block if condition3 is true then block 3 will executed otherwise block4 will be executed.

Example:

void main()
{

int a,b,c,d,max;
printf(“Enter 4 values:”);
scanf(“%d%d%d%d%d”,&a,&b,&c,&d);
if(a>b&&a>c&&A>d)
{
max=a;
}
else
{
if(b>c&&b>d)
{
max=b;
}
else
{if(c>d)
max=c;
else max=d;
}
}
printf(“Maximum value:”,%d,max);
getch();
}

Powered by k2schools