Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Input/Output statements
  5. Decision making statement...
  6. Nested if-else statement

Nested if-else statement

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

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments