If Else If Ladder - C Tutorial

If Else If Ladder

The if else if ladder statement in C programming language is used to test set of conditions in sequence. An if condition is tested only when all previous if conditions in if-else-if ladder is false. If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else-if ladder.

Syntax of if else if ladder statement

if(boolean_expressionA)
{
   //executes when the boolean expressionA is true
}
else if(boolean_expressionB)
{
   //executes when the boolean  expressionB is true
}
else if(boolean_expressionC)
{
   //executes when the boolean  expressionC is true
}
else 
{
   //executes when the all the boolean expression is false
}

If Else If Ladder - Example

 #include<stdio.h>
int main()
{
    float cp, sp;
    printf("Enter Cost Price :");
    scanf("%f",&cp);
    printf("Enter Selling Price :");
    scanf("%f",&sp);
    if (sp > cp)
            printf("Got Profit : %f", sp - cp);
    else if (cp > sp)
            printf("Got Loss :%f", cp - sp);
    else
            printf("Got No Profit No Loss");
    return 0;
}
Output
Enter Cost Price :1000                                                                                                
Enter Selling Price :2000                                                                                             
Got Profit : 1000.000000