C++ 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 <iostream.h>
#include <conio.h>

void main()
{
int x;
int y;

clrscr();

cout << "\nEnter X : ;
cin >> x;

cout << "\nEnter Y : ;
cin >> y;

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

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

Output :

0 comments:

Post a Comment