The general form of a nested if...else statement is,
if(expression)
{
if(expression1)
{
statement-block1;
}
else
{
statement-block2;
}
}
else
{
statement-block3;
}
if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to perform the test for 'expression1' . If the 'expression1' is true the 'statement-block1' is executed otherwise 'statement-block2' is executed.
//Write a C Program which demonstrate use of nested if-else statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 number to compare: ");
scanf("%d%d%d",&a,&b,&c);
if(a > b)
{
if(a > c)
{
printf("a is greatest");
}
else
{
printf("c is greatest");
}
}
else
{
if(b > c)
{
printf("b is greatest");
}
else
{
printf("c is greatest");
}
}
getch();
}
Ask Question