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 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;
}
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 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;
}
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 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;
}
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 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;
}
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 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;
}
Login to try C/C++/Java/PHP code in the editor
- इतने ifs nest करना कि code पढ़ना मुश्किल हो, जब एक condition में
&&काम कर देता। - braces गलत जगह रखना ताकि एक
elseगलत innerifसे attach हो जाए। - एक inner block का closing brace भूल जाना, real problem से दूर compile errors cause करते हुए।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: