Preview
if else if ladder statement in c language | ladder if statement in C
It is basically a multi way decision making statement. In if else if ladder statement, there are multiple conditions one after another. Depending upon these conditions, specific sets of statements are executed.
The syntax of if else if ladder statement is:
if(Test1)
{
Block-1;
}
else if(Test2)
{
Block-2;
}
else if (Test3)
{
Block-3;
}
:
:
else
{
Block-N;
}
Test1, Test2, Test3 are various conditions.
Initially Test1 is checked. If it is true, Block-1 of statements will get executed. If Text1 is false, Test2 is checked. If Test-2 is true Block2 of statements gets executed. If Test2 is false, Test3 will be checked. If Test-3 is true Block3 of statements gets executed.
Block-N will be executed if all conditions are false.
Program to demonstrate the use of if else if ladder statement in C. |
#include<stdio.h> int main() { int a; printf("\nEnter a number :="); scanf("%d",&a); if(a>0) printf("\nValue is positive"); else if(a<0) printf("\nValue is negative"); else printf("\nValue is zero"); return 0; } |
Output |
Enter a value :=-5 Value is negative |
In the above program, a has been entered 6. Condition if(a>0) is true, printf(“\nValue is positive”) would be executed.