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

In if else if ladder statement, multiple conditions are defined one after another .  Different sets of statements are executed depending upon these conditions.

Syntax :

if(Condition-Expression1)
{
Statements-1;
}
else if(Condition-Expression2)     
{
Statements-2;
}
else if (Condition-Expression3)
{
Statements-3;
}
:
:
else     
{
Statements-N;
}
Initially Condition-Expression1 is tested. Statements-1 is executed if it is true. If it evaluates to false, Condition-Expression2 is tested. If it is true Statements-2 is executed. If Condition-Expression2 is false, Condition-Expression3 is tested. If it is true Statements-3 is executed.

Statements-N is executed if all conditions are false.

Statements1, Statements2, Statements3 and Statements4 are set of valid statements of C Language.



 Program 
#include<stdio.h>
int main()
{
int a;
printf(“\nEnter a value :=”);
scanf(“%d”,&a);
if(a>0)
printf(“\nPositive value”);
else if(a<0)
printf(“\nNegative value”);
else
printf(“\nZero”);
return 0;
}
Output
Enter a value :=6
Positive value

In the above program, a has been entered 6.  Condition if(a>0) is true, so printf(“\nValue is positive”) is 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