Nested और Compound Conditionals
In this page:
if [[ condition1 && ( condition2 || condition3 ) ]]; then
commands
fi
एक [[ ]] में Conditions Combine करना
Multiple conditions को && और || उपयोग करके सीधे एक single [[ ]] test के अंदर combine किया जा सकता है, जब checks independent हों तो nested if blocks की ज़रूरत से बचते हुए। यह related logic को visually साथ grouped रखता है।
उदाहरण: Combining Conditions in One [[ ]]
#!/bin/bash
age=25
has_ticket=true
if [[ $age -ge 18 && $has_ticket = true ]]; then
echo "Entry granted"
else
echo "Entry denied"
fi
Login to try C/C++/Java/PHP code in the editor
Parentheses के साथ Grouping
[[ ]] के अंदर, parentheses ( ) sub-expressions को group करके evaluation order explicitly control कर सकते हैं, math expressions जैसा। बिना grouping के, && और || को mix करना एक ऐसे order में evaluate हो सकता है जो आपने intend नहीं किया था।
उदाहरण: Grouping with Parentheses
#!/bin/bash
role="editor"
is_owner=false
if [[ ($role = "admin" || $role = "editor") && $is_owner = false ]]; then
echo "Limited edit access granted"
fi
Login to try C/C++/Java/PHP code in the editor
Nesting if कब ज़्यादा Clear है
कभी-कभी एक बाद की condition को evaluate करना सिर्फ तब sense बनाता है जब एक पहली वाली पहले से pass हो गई हो, खासकर जब दूसरी check अन्यथा error out होगी या meaningless होगी (जैसे एक file की contents जांचना सिर्फ file के exist करने की confirm करने के बाद)।
इन cases में, nested if blocks सब कुछ एक compound expression में cram करने से ज़्यादा clear हैं।
उदाहरण: When Nesting if Is Clearer
#!/bin/bash
file="data.txt"
echo "sample" > "$file"
if [ -f "$file" ]; then
if [ -s "$file" ]; then
echo "$file exists and is non-empty"
else
echo "$file exists but is empty"
fi
else
echo "$file does not exist"
fi
Login to try C/C++/Java/PHP code in the editor
Guard Clauses से Deep Nesting को Flatten करना
कई if blocks nest करने के बजाय, पहले failure cases जांचना और early exit/continue करना (एक 'guard clause') अक्सर flatter, ज़्यादा readable code produce करता है। हर guard main logic चलने से पहले एक specific unwanted condition handle करता है।
उदाहरण: Flattening Deep Nesting with Guard Clauses
#!/bin/bash
check_input() {
local value="$1"
if [ -z "$value" ]; then
echo "Rejected: empty value"
return 1
fi
if [ "$value" -lt 0 ] 2>/dev/null; then
echo "Rejected: negative value"
return 1
fi
echo "Accepted: $value"
}
check_input ""
check_input "-5"
check_input "42"
Login to try C/C++/Java/PHP code in the editor
ifblocks को कई levels गहराई तक over-nest करना जब[[ ]]के अंदर&&/||के साथ एक single compound condition कहीं ज़्यादा readable होगी।- बिना parentheses के
&&और||mix करते समय operator precedence भूल जाना;[[ ]]double brackets के अंदर( )के साथ grouping respect करता है, और इसे skip करना silently बदल सकता है कि असल में कौन सी conditions साथ जांची जा रही हैं। -a/-oके साथ compound conditions के लिए[ ]उपयोग करना; ये deprecated हैं और complex expressions के साथ unreliably behave करते हैं --&&/||के साथ[[ ]]modern, safer choice है।
- Conditions को एक
[[ ]]में&&(and),||(or) उपयोग करके combine किया जा सकता है, और explicit precedence के लिए( )से grouped किया जा सकता है। - Nested
ifblocks तब appropriate हैं जब बाद की checks सिर्फ तब sense बनाती हैं/safe होती हैं जब पहली वाली पहले से pass हो गई हों। [[ ]]sub-expressions को group करने के लिए parentheses support करता है;[ ]इसे reliably support नहीं करता, जो compound logic के लिए[[ ]]prefer करने का एक और कारण है।- Deeply nested conditionals आमतौर पर इशारा करते हैं कि logic को compound
&&/||expressions या earlyreturn/continueguards उपयोग करके flatten किया जा सकता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: