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

C++ में Nested Conditions

एक nested condition एक if statement है जो दूसरे if statement के block के अंदर रखा गया है, इसलिए inner check तभी चलता है जब outer condition पहले ही satisfy हो चुकी हो।
Syntax
cpp
if (outer_condition) {
  if (inner_condition) {
    // runs when both are true
  }
}

Nested Condition क्या है?

एक nested condition एक if statement है जो दूसरे if statement की body के अंदर रखा गया है, और inner condition तभी evaluate होती है जब outer condition पहले ही satisfy हो चुकी हो।

उदाहरण: 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

एक basic nested if में, outer if statement के block में एक पूरा inner if statement होता है, इसलिए innermost code तक पहुँचने के लिए हर enclosing condition का true होना ज़रूरी है।

उदाहरण: 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

एक else branch खुद एक पूरा if-else statement रख सकता है, nested conditions को दो से ज़्यादा outcomes में फर्क करने देते हुए, जैसे तीन अलग age categories अलग करना।

उदाहरण: 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;
}

Nesting के कई Levels

Criteria के बढ़ते specific combinations check करने के लिए conditions को कई levels गहरा nest किया जा सकता है, हालांकि बहुत गहरी nesting code को significantly ज़्यादा पढ़ना और maintain करना मुश्किल बना देती है।

उदाहरण: 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 बनाम Logical Operators

एक nested if वही result achieve करता है जो एक single if statement में logical AND operator (&&) से conditions combine करना, और logical-operator version को simple combined checks के लिए अक्सर ज़्यादा concise और readable माना जाता है।

उदाहरण: 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. #}
आम गलतियां
  1. इतने ifs nest करना कि code पढ़ना मुश्किल हो, जब एक condition में && काम कर देता।
  2. braces गलत जगह रखना ताकि एक else गलत inner if से attach हो जाए।
  3. एक inner block का closing brace भूल जाना, real problem से दूर compile errors cause करते हुए।

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.