if-else-if ladder Statement in C Language

 

If – else - if Statement in C Language

if-else-if ladder Statement in C Language



What is if-else-if ladder statement in C?

if- else- if statement is also known as if-else-if ladder statement. This statement is used when there are more than two possible actions based on different conditions.

The conditional expressions are evaluated from to downward. The condition found true the statement associated with it is executed.

Syntax of if else if ladder statement in C:

if(test condition1)

{

//statement1

}

else

if(test condition2)

{

//statement2

}

else

if(test condition3)

{

//statement 3

}

.

.

......

else

if(test conditionN)

{

//statement N

}

else

{

//default statement to be executed

}


Working of if-else-if ladder statement:

if test condition1 is true then statement1 will be executed. If test condition1 is false then control goes to test condition2 if test condition2 is true then statement2 is executed. If test condition2 is false then control goes to the test condition3. If test condition3 is true then statement3 is executed. If test conditionN is true then then statementN is executed. If test conditionN is false then control goes to else block to execute default statement.


Following example shows the use of if-else-if ladder statement.

/************************************************

C Program to find bigger number among three numbers

************************************************/


#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;

clrscr();
printf("Demonstration of nested if-else statement\n");
printf("\t\t***Input***\n");
printf("Enter three numbers\n");
scanf("%d%d%d",&n1,&n2,&n3);
printf("\t\t***Output***\n");
if(n1>n2&&n1>n3)
printf("First number:%d is bigger\n",n1);
else
if(n2>n1&&n2>n3)
printf("The second number:%d is bigger\n",n2);
else
if(n3>n1&&n3>n2)
printf("The third number: %d is bigger\n",n3);
else
printf("Three numbers:\t%d\t%d\t%d are equal\n",n1,n2,n3);
printf("End of program");
}


Demonstration of nested if-else statement

***Input***

Enter three numbers

12 12 12

***Output***

Three numbers: 12 12 12 are equal

End of program



/************************************************

C Program to find bigger number among three numbers

************************************************/

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;

clrscr();
printf("Demonstration of nested if-else statement\n");
printf("\t\t***Input***\n");
printf("Enter three numbers\n");
scanf("%d%d%d",&n1,&n2,&n3);
printf("\t\t***Output***\n");
if(n1>n2&&n1>n3)
{
printf("First number:%d is bigger\n",n1);
}
else
if(n2>n1&&n2>n3)
{
printf("The second number:%d is bigger\n",n2);
}
else
if(n3>n1&&n3>n2)
{
printf("The third number: %d is bigger\n",n3);
}
else
{
printf("Three numbers:\t%d\t%d\t%d are equal\n",n1,n2,n3);
}
printf("End of program");
}

Demonstration of nested if-else statement

***Input***

Enter three numbers

100

50

90

***Output***

First number:100 is bigger

End of program

टिप्पणी पोस्ट करा

0 टिप्पण्या