Else if ladder in c programming language

If you are searching for else if ladder in c programming then you are at the right blog post.

What is else if ladder in c programming language?

Else if ladder in c is used to take multiple conditions for multiple conditions.

If none of the conditions becomes true then final else block in else if ladder in c is going to be executed.

Here the if statement works from top to down.

else if ladder control flow diagram

General form

if (condition)
    statement 1;
else if (condition)
    statement 2;
.
.
else
    statement x;

Here if the condition is checked. If that condition becomes true statement 1 is executed otherwise else if the condition is checked. If it becomes true statement 2 will be executed otherwise next else if the condition is checked. This process continues until the condition becomes true.

If there are multiple else if statements are passed then the else if ladder becomes complex.

Else if ladder example in c

Example 1

//Else if ladder in c example1.
#include<stdio.h>
int main()
{
  int x;
  printf("\n\n\tEnter the value of x from 1 to 5 \n\t");
  scanf("%d",&x);
  if(x==1) 
     printf("\n\tYou have entered 1 \n\n");
     else if(x==2)
        printf("\n\tYou have entered 2 \n\n");
        else if(x==3)
           printf("\n\tYou have entered 3 \n\n");
            else if(x==4)
              printf("\n\tYou have entered 4 \n\n");
              else if(x==4)
                 printf("\n\tYou have entered 5 \n\n");
                else
                  printf("\n\tYou have entered x outside range of 1 and 5\n\n");

   return 0;
}
      
      
Else if ladder example 1 in c
Else if ladder example 1 in c

Explanation

In the above example suppose I entered value 3 then the first if the condition becomes false. So it moves to a second condition where it also becomes false. So it moves to the next else if condition and it becomes true therefore it prints “you have entered 3 “.

Example 2

//Else if ladder in c example2.
#include<stdio.h>
int main(int argc, char const *argv[])
{
  int number;
  printf("\n\n\tEnter a number\n\t");
  scanf("%d",&number);
  if( number%2 == 0 )
  {
    printf("\n\t%d is completely divisible by 2\n\n",number);
  }
  else if(  number%3 == 0 )
  {
    printf("\n\t%d is completely divisible by 3\n\n",number);
  }
  else if(  number%4 == 0 )
  {
    printf("\n\t%d is completely divisible by 4\n\n",number);
  }
  else if(  number%5 == 0 )
  {
    printf("\n\t%d is completely divisible by 5\n\n",number);
  }
  else if(  number%6 == 0 )
  {
    printf("\n\t%d is completely divisible by 6\n\n",number);
  }
  else if(  number%7 == 0  )
  {
    printf("\n\t%d is completely divisible by 7\n\n",number);
  }
  else if(  number%8 == 0 )
  {
    printf("\n\t%d is completely divisible by 8\n\n",number);
  }
  else if(  number%9 == 0 )
  {
    printf("\n\t%d is completely divisible by 9\n\n",number);
  }
  else
  {
    printf("\n\t%d is not completely divisible\n\n",number);
  }
  return 0;
}
Else if ladder example 2 in c
Else if ladder example 2 in c

Explanation

Here we are inputting a number using scanf.

If the number is completely divisible by each number from 2 to 9 then we are printing it as the inputted number is completely divisible by that number.

Try inputting 45. It shows as 45 is completely divisible by 3. It’s because 3 is first checked before then the remaining if-else statements won’t execute at all.


Difference between if and else if ladder

If statement in c executes only true block statement. else if ladder in c checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.

All the statements in if block is executed if the condition is met otherwise no statements will be executed. else if ladder checks for all conditions to find out true block otherwise final else block is executed.

In if statement if is a keyword. In else if ladder if,else else if are keywords.

There is no complexity in simple if statement. As the true condition goes deeper else if ladder becomes complex statement.

General forms are as follows

//if block
if(condition is true)
    execute the statement;

//else if ladder block
if (condition)
    statement 1;
else if (condition)
    statement 2;
.
.
else
    statement x;
simple if in celse if ladder in c
Executes only true block statement.Checks multiple conditions and executes true block otherwise final else block i.e. false block is executed.
All the statements in if block is executed if condition is met otherwise no statements will be executed.Checks for all conditions to find out true block otherwise final else block is executed.
There is no complexity.As the true condition goes deeper else if ladder becomes complex statement.
if(condition is true)
execute the statement
;
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement x;

Difference between else if and else if ladder

If else executes either true block or false block statement. else if ladder in c checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.

In if else any one block is executed. else if ladder in c checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.

In if else if and else are keywords. In else if if,else, else if are keywords.

if else is a extended form of if statement. Else if is extended form of if else statement.

General forms are as follows

//if else block
if (condition met)
   {
     true block ;
   }
else 
   {
     false block;
   }

//else if ladder block
if (condition)
    statement 1;
else if (condition)
    statement 2;
.
.
else
    statement x;
if else in celse if ladder in c
Executes either true block or false block statement.Checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.
Any one block is executed.Checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.
if and else are keywords.else if if,else, else if are keywords.
if (condition met)
{
true block ;
}
else
{
false block;
}
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement x;

Difference between switch and else if ladder.

In switch the matched case block is executed. else if ladder in c checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.

In switch, any of the matched case blocks is executed otherwise default statement is executed. else if ladder in c checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.

In switch statement switch,case,default are keywords. In else if if,else, else if are keywords.

In switch there is no complexity. In else if ladder there is a complexity if condition becomes deeper.

General forms are as follows

// switch block
switch( integer expression)
  {
     case constant 1: block1;
     case constant 2: block2;
      .
      .
     default:
              default-block;
  }   


//else if ladder block
if (condition)
    statement 1;
else if (condition)
    statement 2;
.
.
else
    statement x;          
switch in celse if ladder in c
The matched case block is executed.Checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.
switch,case,default are keywords.if,else, else if are keywords.
There is no complexity.There is a complexity if condition becomes deeper.
switch( integer expression)
{
case constant 1: block1;
case constant 2: block2;
.
.
default:
default-block;
}
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement x;

People also ask for

What is else if ladder in c?

else if ladder in c checks for multiple conditions and executes true block otherwise final else block i.e. false block is executed.

Which one is better between switch and else if ladder?

Switch statement becomes simpler than else if ladder. But the switch statement is not flexible as else if ladder.

What is nesting in c?

We can place if statement within another if statement this is called nested if statement in c.


People also read