Pages

Subscribe:

Labels

Tuesday, 18 December 2012

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 :

Enter X : 3

Enter Y : 3

Bot Are Same

0 comments:

Post a Comment