← Back to Swift Course | Chapter 3: Control Flow | Lesson 1 of 8

If-Else Statements क्या हैं

एक if-else statement आपके program को एक decision लेने देता है, जैसे कोई चीज़ true होने पर एक path और नहीं होने पर दूसरा path चुनना।
Syntax
swift
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

markup
// 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")
}

if-else

एक else clause जोड़ना एक fallback block देता है जो तब चलता है जब if condition false हो।

उदाहरण: if-else

markup
// 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")
}

else if Chains

कई conditions को else if उपयोग करके order में जांचा जा सकता है, पहले true वाले पर रुकते हुए।

Note: Swift हर branch body के आस-पास curly braces चाहता है, यहां तक कि एक single-line वाली भी।

उदाहरण: else if Chains

markup
// 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")
}
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. Condition को if (x > 5) जैसे parentheses में wrap करना, जो अनावश्यक और non-idiomatic Swift है (हालांकि harmless)।
  2. एक single-statement body के आस-पास curly braces भूल जाना; Swift इन्हें चाहता है, कुछ C-style languages के विपरीत।
  3. कई अलग if statements chain करना जब एक single else if chain (या switch) ज़्यादा clear होता।
चैप्टर सारांश
  • if एक block सिर्फ तब execute करता है जब इसका condition true हो।
  • else if sequence में अतिरिक्त conditions जांचता है, और else तब चलता है जब कोई match न हो।
  • Curly braces { } Swift में mandatory हैं, यहां तक कि एक single statement के लिए भी।
  • Conditions असली Bool expressions होने चाहिए -- Swift एक Int को condition के रूप में accept नहीं करेगा।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.