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

Switch Statement के Basics

एक switch statement एक value को possible matches की एक list से जांचता है और जो भी fit हो उसके लिए code चलाता है।
Syntax
swift
switch value {
case pattern1:
    // code
case pattern2, pattern3:
    // code
default:
    // code
}

Basic Switch Syntax

एक switch statement एक value को एक बार evaluate करता है और इसे case patterns की एक series से match करता है, सिर्फ matching block चलाते हुए।

उदाहरण: Basic Switch Syntax

markup
// Declare a constant `day`, set to `3`
let day = 3
// Switch on `day` and branch by case
switch day {
// Case: `1`
case 1:
    // Print "Monday" to the console
    print("Monday")
// Case: `2`
case 2:
    // Print "Tuesday" to the console
    print("Tuesday")
// Case: `3`
case 3:
    // Print "Wednesday" to the console
    print("Wednesday")
default:
    // Print "Some other day" to the console
    print("Some other day")
}

Matching Multiple Values in One Case

एक single case commas से अलग करके एक साथ कई values match कर सकता है।

उदाहरण: Matching Multiple Values in One Case

markup
// Declare a constant `letter` (a Character), set to `"a"`
let letter: Character = "a"
// Switch on `letter` and branch by case
switch letter {
// Case: `"a", "e", "i", "o", "u"`
case "a", "e", "i", "o", "u":
    // Print "\(letter) is a vowel" to the console
    print("\(letter) is a vowel")
default:
    // Print "\(letter) is a consonant" to the console
    print("\(letter) is a consonant")
}

No Implicit Fallthrough

C या Java के विपरीत, Swift के switch cases default रूप से अगले case में fall through नहीं होते -- सिर्फ matching case का code चलता है।

Note: अगर आप सच में fallthrough behavior चाहते हैं, किसी case के आखिर में explicitly fallthrough keyword जोड़ें।

उदाहरण: No Implicit Fallthrough

markup
// Declare a constant `number`, set to `2`
let number = 2
// Switch on `number` and branch by case
switch number {
// Case: `1`
case 1:
    // Print "one" to the console
    print("one")
// Case: `2`
case 2:
    // Print "two" to the console
    print("two")
// Case: `3`
case 3:
    // Print "three" to the console
    print("three")
default:
    // Print "other" to the console
    print("other")
}
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. यह भूल जाना कि Swift के switch को exhaustive होना चाहिए -- हर possible value handle होनी चाहिए, अक्सर एक default case की ज़रूरत पड़ती है।
  2. C की तरह cases के बीच implicit fallthrough की उम्मीद करना; Swift cases अपने आप fall through नहीं होते जब तक आप explicitly fallthrough न लिखें।
  3. एक साधारण two-way boolean check के लिए switch उपयोग करना जहां एक if-else simpler और clearer होता।
चैप्टर सारांश
  • switch एक value की तुलना कई case patterns से करता है और matching वाला चलाता है।
  • Swift switches को exhaustive होना चाहिए; एक default case बाकी बची किसी भी values को cover करता है।
  • C के विपरीत, cases अपने आप अगले case में fall through नहीं होते।
  • एक comma-separated list उपयोग करके एक case में कई values match की जा सकती हैं।
🔒

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.