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

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(Condition-1)
{
Statement-Block-1;
}
else if(Condition-2)     
{
Statement-Block-2;
}
else if (Condition-3)
{
Statement-Block-3;
}
:
:
else     
{
Statement-Block-N;
}
Initially Condition-1 is checked. If it is true, Statement-Block-1 will be executed. If Condition-1 is false, Condition-2 will be checked. If Condition-2 is true Statement-Block2 will be executed. If Condition-2 is false, Condition-3 will be checked.

Statement-Block-N will be executed if all conditions are false.



 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

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





Best Books of C

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