C Nested Conditions
In this page:
if (outer_condition) {
if (inner_condition) {
// runs when both are true
}
}
What is a Nested Condition?
A nested condition is an if statement placed inside the body of another if statement, and the inner condition is only evaluated at all once the outer condition has already been satisfied.
Example: What is a Nested Condition?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Check whether age >= 18
if (age >= 18) {
// Check whether age < 65
if (age < 65) {
// Print formatted text to the screen
printf("Working age");
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Basic Nested if
In a basic nested if, the outer if statement's block contains a complete inner if statement, so reaching the innermost code requires every enclosing condition to be true.
Example: Basic Nested if
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 10
int n = 10;
// Check whether n > 0
if (n > 0) {
// Check whether n % 2 == 0
if (n % 2 == 0) {
// Print formatted text to the screen
printf("Positive even");
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested if-else
An else branch can itself contain a complete if-else statement, letting nested conditions distinguish between more than two outcomes, such as separating three different age categories.
Example: Nested if-else
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 30
int age = 30;
// Check whether age < 13
if (age < 13) {
// Print formatted text to the screen
printf("Child");
} else {
// Check whether age < 20
if (age < 20) {
// Print formatted text to the screen
printf("Teen");
} else {
printf("Adult");
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Levels of Nesting
Conditions can be nested several levels deep to check increasingly specific combinations of criteria, though very deep nesting tends to make code significantly harder to read and maintain.
Example: Multiple Levels of Nesting
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 5
int a = 5;
// Check whether a > 0
if (a > 0) {
// Check whether a < 10
if (a < 10) {
if (a % 2 != 0) {
// Print formatted text to the screen
printf("Positive, single-digit, odd");
}
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Conditions vs Logical Operators
A nested if achieves the same result as combining conditions with the logical AND operator (&&) in a single if statement, and the logical-operator version is often considered more concise and readable for simple combined checks.
Example: Nested Conditions vs Logical Operators
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 5
int a = 5;
// Check whether a > 0
if (a > 0) {
// Check whether a < 10
if (a < 10) {
// Print formatted text to the screen
printf("Nested version");
}
}
if (a > 0 && a < 10) {
// Print formatted text to the screen
printf("Logical operator version");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Nesting so many
ifblocks that the code becomes hard to read, when&&would do. - Misplacing braces so an
elseattaches to the wrong innerif. - Forgetting a closing brace for an inner block.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: