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

C में Nested Conditions

एक nested condition एक if statement है जो दूसरे if statement के block के अंदर रखा गया है, इसलिए inner check तभी चलता है जब outer condition पहले ही satisfy हो चुकी हो।
Syntax
c
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?

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

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

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

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

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

Nesting के कई Levels

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

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

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

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.