C Nested if
If statement inside the body of another if statement is called the nested if statement.
The first if statement is called the outer if statement and the second if statement inside the outer if statement is called as inner if statement.
First the outer if condition is checked and if it is correct then the control goes in the inner if condition and if this is correct too then the inner statement respective output is generated.
Syntax :
if(condition)
{
if(condition)
{
statements to exceute
}
statements to execute
}
Example:
#include <stdio.h>
#include <conio.h>
void main()
{
int x=5;
int y=2;
int z=1;
clrscr();
if(x>y); //outer if
{
if(x>z) //inner if
{
printf("x is greater than both y and z");
}
}
getch();
}
Output:
Description:
The outer condition is true so the control goes inside outer if body and it will check for the inner if condition, since it is also true hence the inner if statement is executed and the output is displayed.
Prev NEXT
0 comments:
Post a Comment