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

   //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
   //executes when the all the boolean expression is false

If Else If Ladder - Example

int main()
    float cp, sp;
    printf("Enter Cost Price :");
    printf("Enter Selling Price :");
    if (sp > cp)
            printf("Got Profit : %f", sp - cp);
    else if (cp > sp)
            printf("Got Loss :%f", cp - sp);
            printf("Got No Profit No Loss");
    return 0;
Enter Cost Price :1000                                                                                                
Enter Selling Price :2000                                                                                             
Got Profit : 1000.000000