If-Else Statements क्या हैं
एक if-else statement आपके program को एक decision लेने देता है, जैसे कोई चीज़ true होने पर एक path और नहीं होने पर दूसरा path चुनना।
In this page:
Syntax
if condition {
// runs when true
} else if otherCondition {
// runs when this is true
} else {
// fallback
}
Basic if Statement
एक if statement अपना code block सिर्फ तब चलाता है जब parentheses-free header के अंदर condition true evaluate होता है।
उदाहरण: Basic if Statement
// Declare a constant `temperature`, set to `30`
let temperature = 30
// Check whether `temperature > 25`
if temperature > 25 {
// Print "It is hot outside" to the console
print("It is hot outside")
}
Login to try C/C++/Java/PHP code in the editor
if-else
एक else clause जोड़ना एक fallback block देता है जो तब चलता है जब if condition false हो।
उदाहरण: if-else
// Declare a constant `temperature`, set to `15`
let temperature = 15
// Check whether `temperature > 25`
if temperature > 25 {
// Print "It is hot outside" to the console
print("It is hot outside")
// Otherwise, run this branch
} else {
// Print "It is not that hot" to the console
print("It is not that hot")
}
Login to try C/C++/Java/PHP code in the editor
else if Chains
कई conditions को else if उपयोग करके order में जांचा जा सकता है, पहले true वाले पर रुकते हुए।
Note:
Swift हर branch body के आस-पास curly braces चाहता है, यहां तक कि एक single-line वाली भी।
उदाहरण: else if Chains
// Declare a constant `score`, set to `72`
let score = 72
// Check whether `score >= 90`
if score >= 90 {
// Print "Grade: A" to the console
print("Grade: A")
// Otherwise, if `score >= 70`, run this branch
} else if score >= 70 {
// Print "Grade: B" to the console
print("Grade: B")
// Otherwise, run this branch
} else {
// Print "Grade: C or below" to the console
print("Grade: C or below")
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- Condition को
if (x > 5)जैसे parentheses में wrap करना, जो अनावश्यक और non-idiomatic Swift है (हालांकि harmless)। - एक single-statement body के आस-पास curly braces भूल जाना; Swift इन्हें चाहता है, कुछ C-style languages के विपरीत।
- कई अलग
ifstatements chain करना जब एक singleelse ifchain (याswitch) ज़्यादा clear होता।
चैप्टर सारांश
ifएक block सिर्फ तब execute करता है जब इसका condition true हो।else ifsequence में अतिरिक्त conditions जांचता है, औरelseतब चलता है जब कोई match न हो।- Curly braces
{ }Swift में mandatory हैं, यहां तक कि एक single statement के लिए भी। - Conditions असली
Boolexpressions होने चाहिए -- Swift एकIntको condition के रूप में accept नहीं करेगा।
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: