Nested if statement in C Langauge

Preview

Nested if statement in C Langauge

Nested if statement contains one if statement block taken inside another if statement. The syntax of nested if statement is:
if(Condition-Expression1)
{
            if(Condition-Expression2)
            Statements1;
            else     
            Statements2;
}
else
{
            if(Condition-Expression3)
            Statements3;
            else     
            Statements4;
}
Initially Condition-Expression1 is checked. If it is true, Condition-Expression2 is checked. If it is also true, Statements1 is executed otherwise Statements2 is executed. If ConditionExpression1 as false, Condition-Expression3 is checked. If it is true, Statements3 will be executed. otherwise Statements4 will be executed.

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>=100)
{
if(a<=200)
printf(“\nBetween 100 and 200 “);
}
else
printf(“\nSmaller than 100”);
getch();
}
Output
Enter a value:=130
Between 100 and 200

In the above program, a has been entered 130. 

if(a>=100) is true so if(a<=200) is checked. It is also true so printf(“\nBetween 100 and 200”) is executed. 



Lesson tags: if types in c, nested if in c, nested if statement in c, program of nested if
Back to: C Programming Language