Home » Blog » else if Ladder In C

else if Ladder In C

Decision making with if statement may be implemented in different forms depending on the complexity of conditions to be tested.The different forms are,

  1. Simple if statement
  2. if….else statement
  3. Nested if….else statement
  4. Using else if statement

Syntax

if(expression1)
{
    statement block1;
}
else if(expression2) 
{
    statement block2;
}
else if(expression3 ) 
{
    statement block3;
}
else 
{
    default statement;
}

The expression is tested from the top(of the ladder) to downwards. As soon as a true condition is found, the statement associated with it is executed.

Example is shown below ::

#include <stdio.h>
#include<conio.h>

void main( )
{
    int a;
	
    clrscr();
	
    printf("Enter a number...");
    scanf("%d", &a);
	
    if(a%5 == 0 && a%8 == 0)
    {
        printf("Divisible by both 5 and 8");
    }  
    else if(a%8 == 0)
    {
        printf("Divisible by 8");
    }
    else if(a%5 == 0)
    {
        printf("Divisible by 5");
    }
    else 
    {
        printf("Divisible by none");
    }
	
    getch();
}

OutPut

Enter a number... 2
	
Divisible by none

It’s Done. Keep It Up Guys…

Leave a Reply

Your email address will not be published.