else if Ladder Statement in C
Notes:
else if Ladder Statement in C Programming Language:
else if ladder statement: looks like a ladder
- helps us to check multiple conditional expressions
syntax:
if(conditional expression1) {
statements; // true part for if (conditional expression 1)
}
else if(conditional expression2) {
statements; // true part for if (conditional expression 2)
}
else if(conditional expression3) {
statements; // true part for if (conditional expression 3)
}
…
else {
statements; // false part for last conditional expression
// if no conditional expression evaluates to true, then the last else block is executed
}
Ex: Program to display equivalent day name for the given day number using else if ladder statement
#include <stdio.h>
int main()
{
int day = 4;
if(day==1)
{
printf("Sunday\n");
}
else if(day==2)
{
printf("Monday\n");
}
else if(day==3)
{
printf("Tuesday\n");
}
else if(day==4)
{
printf("Wednesday\n");
}
else if(day==5)
{
printf("Thursday\n");
}
else if(day==6)
{
printf("Friday\n");
}
else if(day==7)
{
printf("Saturday\n");
}
else
{
printf("Invalid day\n");
}
return 0;
}