Table of Contents
A nested if statement places one if statement inside another. The inner condition is evaluated only when the outer condition is true.
#include <stdio.h>
int main()
{
int number = 10;
if (number > 0)
{
if (number % 2 == 0)
{
printf("The number is positive and even.\n");
}
}
return 0;
}
Example Output #
The number is positive and even.
Explanation #
The outer if checks whether number is positive:
if (number > 0)
Since the condition is true, execution enters the outer block and evaluates the inner if:
if (number % 2 == 0)
The % operator produces the remainder after division. Since 10 % 2 is 0, the inner condition is also true and the printf() statement executes.
The inner if therefore depends on the outer if being satisfied.