← Back to C++ Course | Chapter 4: Control Flow | Lesson 6 of 13

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
cpp
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?

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare outer and set it to true
	bool outer = true;
	// Check whether outer
	if (outer) {
		// Declare inner and set it to true
		bool inner = true;
		// Check whether inner
		if (inner) {
			// Print to the console with cout
			std::cout << "Inner reached" << std::endl;
		}
	}
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Declare hasTicket and set it to true
	bool hasTicket = true;
	// Check whether age >= 18
	if (age >= 18) {
		// Check whether hasTicket
		if (hasTicket) {
			// Print to the console with cout
			std::cout << "Entry allowed" << std::endl;
		}
	}
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 75
	int score = 75;
	// Check whether score >= 90
	if (score >= 90) {
		// Print to the console with cout
		std::cout << "A" << std::endl;
	} else {
		// Check whether score >= 70
		if (score >= 70) {
			// Print to the console with cout
			std::cout << "B" << std::endl;
		} else {
			std::cout << "C" << std::endl;
		}
	}
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare loggedIn and set it to true
	bool loggedIn = true;
	// Declare isAdmin and set it to true
	bool isAdmin = true;
	bool hasPermission = true;
	// Check whether loggedIn
	if (loggedIn) {
		// Check whether isAdmin
		if (isAdmin) {
			if (hasPermission) {
				// Print to the console with cout
				std::cout << "Access granted" << std::endl;
			}
		}
	}
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Declare hasTicket and set it to true
	bool hasTicket = true;

	// Check whether age >= 18
	if (age >= 18) {
		// Check whether hasTicket
		if (hasTicket) {
			// Print to the console with cout
			std::cout << "Nested: allowed" << std::endl;
		}
	}

	if (age >= 18 && hasTicket) {
		// Print to the console with cout
		std::cout << "Logical AND: allowed" << std::endl;
	}
	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 ifs that the code is hard to read, when && in one condition would do.
  2. Misplacing braces so an else attaches to the wrong inner if.
  3. Forgetting the closing brace of an inner block, causing compile errors far from the real problem.

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.