Header Ads Widget

Responsive Advertisement

If -else if ladder with Syntax and Example in C

There are following types of conditional statements in C :
  • if statement
  • if - else statement
  • nested if-else statement
  • if -else if ladder
  • switch statement

4. If-else if ladder - The statements inside the body of “if” only execute if the given condition returns true, if not then the "else if"condition is checked , then if the true is returned then "else if" condition is executed and if the condition returns false then the statements inside “else” is executed.

Syntax :

if (condition)
       // Body of the loop.
       // These statements will only execute if the condition is true.
}
else if(condition)
{
       // Body of the loop.
       // These statements will only execute if the condition is true.
}
else
       // Body of the loop.
       // These statements will only execute if all the above conditions are false.
}


Flow Diagram of If-else if ladder

    
       if statement in javascript


Example of If-else if ladder in C


#include<stdio.h>

#include<conio.h>

int main()
{
   int  a=15,b=10;
   if(a<b)  //if condition statement
    {
       printf("Value of a is less than b");  //display output
    }
   else if(a>b)  //else if condition statement
    {
       printf("Value of b is less than a."); //display output
    }
   else  //else condition statement
    {
       printf("Value of a is equal to b."); //display output
    }
  getch();
}


OUTPUT:

Value of b is less than a.

Post a Comment

0 Comments