Ladder if or else if statement


C Ladder If or else if statement


When in any situation number of conditions arise in a sequence, then we can use ladder-if
statement.
In this, first condition is checked. If it is true then action will be taken. Otherwise further
action will be checked and this process will continue till the end of the condition.

The general syntax of this is :

if(condition 1)
{
statement1
}

else if(condition 2)
{
statement 2;
}

else if(condition 3)
{
statement 3;
}
........................
........................

else if(condition n)
{
statement-n;
}
else
{
default statement;
}
Statement -x


Example :


#include <stdio.h>
#include <conio.h>

void main()
{
int x;
int y;

clrscr();

printf("\nEnter X : ");
scanf("%d",&x);

printf("\nEnter Y : ");
scanf("%d",&y);

if ( x > y )
{
printf("\nX is Greater Than Y");
}

else if ( y > x)
{
printf("\nY is Greater Than X");
}
else
{
printf("\nBoth Are Same");
}
getch();
}

Output :











     Prev                                                   NEXT

0 comments:

Post a Comment