← Back to C Course | Chapter 4: Control Flow | Lesson 4 of 11

C Nested Conditions

A nested condition is an if statement placed inside another if statement's block, so the inner check only runs once the outer condition has already been satisfied.
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Nesting so many if blocks that the code becomes hard to read, when && would do.
  2. Misplacing braces so an else attaches to the wrong inner if.
  3. Forgetting a closing brace for an inner block.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.