C में Nested Conditions
In this page:
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?
// 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
एक basic nested if में, outer if statement के block में एक पूरा inner if statement होता है, इसलिए innermost code तक पहुँचने के लिए हर enclosing condition का true होना ज़रूरी है।
उदाहरण: 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
एक else branch खुद एक पूरा if-else statement रख सकता है, nested conditions को दो से ज़्यादा outcomes में फर्क करने देते हुए, जैसे तीन अलग age categories अलग करना।
उदाहरण: 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
Nesting के कई Levels
Criteria के बढ़ते specific combinations check करने के लिए conditions को कई levels गहरा nest किया जा सकता है, हालांकि बहुत गहरी nesting code को significantly ज़्यादा पढ़ना और maintain करना मुश्किल बना देती है।
उदाहरण: 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 बनाम 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
// 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
- इतने
ifblocks nest करना कि code पढ़ना मुश्किल हो जाए, जब&&काम कर देता। - braces गलत जगह रखना ताकि एक
elseगलत innerifसे attach हो जाए। - एक inner block के लिए closing brace भूल जाना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: