C - Nested If Statements
C - Nested If Statements
C - Nested If Statements
It is always legal in C programming to nest if-else statements, which means you can
use one if or else-if statement inside another if or else-if statement(s).
Syntax
The syntax of nested if statements is as follows −
if (expr1){
if (expr2){
block to be executed when
expr1 and expr2 are true
}
else{
block to be executed when
expr1 is true but expr2 is false
}
}
https://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htm 1/7
6/16/24, 11:54 AM C - Nested If Statements
You can compound the Boolean expressions with && or || to get the same effect.
However, for more complex algorithms, where there are different combinations of
multiple Boolean expressions, it becomes difficult to form the compound conditions.
Instead, it is recommended to use nested structures.
Another if statement can appear inside a top-level if block, or its else block, or
inside both.
Example 1
Let us take an example, where the program needs to determine if a given number is
less than 100, between 100 to 200, or above 200. We can express this logic with the
following compound Boolean expression −
#include <stdio.h>
if (a < 100){
printf("Value of a is less than 100\n");
}
https://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htm 2/7
6/16/24, 11:54 AM C - Nested If Statements
if (a >= 200){
printf("Value of a is more than 200\n" );
}
}
Output
Run the code and check its output. Here, we have intialized the value of "a" as 274.
Change this value and run the code again. If the supplied value is less than 100,
then you will get a different output. Similarly, the output will change again if the
supplied number is in between 100 and 200.
Value of a is : 274
Value of a is more than 200
Example 2
Now let's use nested conditions for the same problem. It will make the solution more
understandable when we use nested conditions.
First, check if "a >= 100". Inside the true part of the if statement, check if it is <200
to decide if the number lies between 100-200, or it is >200. If the first condition (a
>= 100) is false, it indicates that the number is less than 100.
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htm 3/7
6/16/24, 11:54 AM C - Nested If Statements
return 0;
}
Output
Run the code and check its output. You will get different outputs for different input
values of "a" −
Value of a is : 120
Value of a is between 100 and 200
Example 3
The following program uses nested if statements to determine if a number is
divisible by 2 and 3, divisible by 2 but not 3, divisible by 3 but not 2, and not
divisible by both 2 and 3.
#include <stdio.h>
int main(){
int a = 15;
printf("a: %d\n", a);
if (a % 2 == 0) {
if (a % 3 == 0){
printf("Divisible by 2 and 3");
}
else {
https://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htm 4/7
6/16/24, 11:54 AM C - Nested If Statements
return 0;
}
Output
a: 15
Divisible by 3 but not 2
Example 4
Given below is a C program to check if a given year is a leap year or not. Whether
the year is a leap year or not is determined by the following rules −
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htm 5/7
6/16/24, 11:54 AM C - Nested If Statements
int main(){
// Test the program with the values 1900, 2023, 2000, 2012
int year = 1900;
printf("year: %d\n", year);
// is divisible by 4?
if (year % 4 == 0){
// is divisible by 100?
if (year % 100 == 0){
// is divisible by 400?
if(year % 400 == 0){
printf("%d is a Leap Year\n", year);
}
else{
printf("%d is not a Leap Year\n", year);
}
}
else{
printf("%d is not a Leap Year\n", year);
}
}
else{
printf("%d is a Leap Year\n", year);
}
}
Output
year: 1900
1900 is not a Leap Year
Test the program with different values for the variable "year" such as 1900, 2023,
2000, 2012.
The same result can be achieved by using the compound Boolean expressions
instead of nested if statements, as shown below −
https://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htm 6/7
6/16/24, 11:54 AM C - Nested If Statements
https://www.tutorialspoint.com/cprogramming/nested_if_statements_in_c.htm 7/7