Table of Contents
show
Nested if-else statement
- In a nested if-else statement, the if body or else body of an if-else statement is, or contains, another if statement or if-else statement
- When s statement contains a greater number of if clauses than else clauses, then there exists a potential ambiguity regarding with which if clause does the else clause properly matches up.
- This ambiguity is known as dangling else problem
Syntax
if(expression)
{
Statement 1
if(expression)
{
Statement 2
}
else
{
Statement 3
}
}
else
{
if(expression)
{
Statement 4
}
else
{
Statement 5
}
}
Flowchart

Example
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers \t");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("%d is greatest",a);
}
else
{
printf("%d is greatest",c);
}
}
else
{
if(b>c)
{
printf("%d is greatest",b);
}
else
{
printf("%d is greatest",c);
}
}
return 0;
}
Output
Enter three numbers 200 100 400
400 is greatest
Views: 0