If – else – if Ladder in C Language

This “if – else – if ladder in C language Tutorial ” post providing complete information including syntax, flow chart and code programs to understand clearly.


 

if-else-if ladder:

  • Defining many if blocks and execute One among defined by check the conditions sequentially.
  • Second if block condition checks only if first condition has failed.
  • If all the conditions are false, execute final and optional else block.
Syntax:

if(condition1)
{
Statements1;
}
else if(condition2)
{
Statements2;
}
else if(condition3)
{
Statements3;
}
else
{
Statements4;
}

 

Biggest of 3 numbers using if-else-if ladder :
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter 3 integers : “);
scanf(“%d%d%d”, &a , &b , &c);
if(a>b && a>c)
printf(“A is big”);
else if(b>a && b>c)
printf(“B is big”);
else if(c>a && c>b)
printf(“C is big”);
}

Note : Above application execution takes more time. We can re-write the program as follows
​#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter 3 integers : “);
scanf(“%d%d%d”, &a , &b , &c);
if(a>b && a>c)
printf(“A is big”);
else if(b>c)
printf(“B is big”);
else
printf(“C is big”);
}

Share this