if else if ladder statement in c language

Preview
You must first complete Simple if statement in C language before viewing this Lesson

if else if ladder statement in c language

This is most commonly used selection statement in C language.  It is basically a multi way decision making statement.

In if else if ladder statement, there are multiple conditions one after another. Depending upon these conditions, specific sets of statements are executed.

The syntax of if else if ladder statement is:

if(Condition1)
{
Statement-Block1;
}
else if(Condition2)     
{
Statement-Block2;
}
else if (Condition3)
{
Statement-Block3;
}
:
:
else     
{
Statement-BlockN;
}
Statement-X;

Condition1,Condition2 and Condition3 are relational or logical expressions which will be checked to see which block of statements should be executed.

Initially Condition1 is checked. If it is true, Statement-Block1 will be executed. If Condition1 is false, Condition2 will be checked.

If Condition2 is true Statement-Block2 will be executed. If Condition2 is false, Condition3 will be checked.

In case Condition3 is true, Statement-Block3 will be executed and so on.

If all the Conditions are false, Statement-BlockN will be executed.

Statement-Block1, Statement-Block2, Statement-Block3 and Statement-BlockN are set of statements, which will be executed depending upon various Conditions. These blocks of statements must contain valid statements of C language.

Statement-X may be a single statement or group of statements which will be executed in both the cases of condition to be true as well as false.

if else if ladder statement can be represented with the help of a flowchart as :

 Program to demonstrate the use of if else if ladder statement.
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\nEnter a value :=”);
scanf(“%d”,&a);
if(a>0)
printf(“\nValue is positive”);
else if(a<0)
printf(“\nValue is negative”);
else
printf(“\nValue is zero”);
getch();
}
Output
Enter a value :=6
Value is positive

Description of above program

In the above program, a has been entered 6.  Condition if(a>0) is true, printf(“\nValue is positive”) would be executed.

Test your Knowledge

Write C programs for the following:

1) The basic salary of an employee is typed through the keyboard.
If basic salary<=5000 then
DA is 45% of basic salary and HRA is 10% of basic salary.
If basic salary>5000  and basic salary <=1000 then
DA is 45.5% of basic salary and HRA is 10.5% of basic salary.
If basic salary>1000 then
DA is 46.5% of basic salary and HRA is 10.9% of basic salary.
Calculate net salary as basic salary+DA+HRA

2) The user should enter the age of a person.
If the age is below 18 the message should be “Person is Minor”.
If the age is between 18 and 40 the message should be “Person is Adult”.
If age is more than 40 the message should be “Person has stepped into maturity”.


Lesson tags: if else if ladder statementr in C, if else if statement in c, ladder if statement in c
Back to: C Programming Language