Nested if statement
The syntax of the nested if is as follows:
if(expression1){
// Executes when the expression 1 is true
if(expression2){
// Executes when the expression 2 is true */
}else{
// Executes when the expression 2 is false */
}
}else{
// statements to be executed if all test expressions are false
}
If expression1 is false, then expression2 is checked if it is true then statements are executed. If expression2 is also gets false, then controller execut else part.
#include
#include
void main()
{
int m, n;
printf("Enter the value of m:");
scanf("%d", &m);
printf("Enter the value of n:");
scanf("%d",&n);
if (m != n)
{
printf("m is not equal to n");
//Nested if
if (m > n)
{
printf("m is greater than n");
}
else
{
printf("n is greater than m");
}
}
else
{
printf("m is equal to n");
}
getch();
}
Output
Enter the value of m:15
Enter the value of n:19
m is not equal to n
n is greater than m