← Back to Kotlin Course | Chapter 3: Control Flow | Lesson 2 of 7

when Expression क्या है

when किसी value को संभावनाओं की एक list से जांचने और मेल खाने वाला option चलाने का Kotlin का तरीका है, जैसे एक समझदार switch statement।
Syntax
kotlin
when (subject) {
    value1 -> action1
    value2, value3 -> action2
    in start..end -> action3
    else -> defaultAction
}

val result = when (subject) { value -> a; else -> b }

Basic when Usage

when एक subject value को branches की एक series से compare करता है और जो पहला मेल खाए उसे execute करता है, लंबी if/else if chains को साफ़ syntax से replace करते हुए।

उदाहरण: Basic when Usage

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `day`, set to `3`
    val day = 3
    // Branch on the value of `day`
    when (day) {
        1 -> println("Monday")
        2 -> println("Tuesday")
        3 -> println("Wednesday")
        else -> println("Some other day")
    }
}

Matching Multiple Values

एक single branch commas से अलग करके एक साथ कई values match कर सकता है, जिन cases के लिए एक जैसा व्यवहार चाहिए उनके लिए दोहराई गई branches से बचते हुए।

उदाहरण: Matching Multiple Values

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `letter`, set to `'a'`
    val letter = 'a'
    // Branch on the value of `letter`
    when (letter) {
        'a', 'e', 'i', 'o', 'u' -> println("Vowel")
        else -> println("Consonant")
    }
}

Matching Ranges

किसी when branch के अंदर in का उपयोग करना यह जांचता है कि value किसी range के भीतर आती है या नहीं, जो कई chained comparisons से कहीं ज़्यादा साफ़ है।

उदाहरण: Matching Ranges

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `score`, set to `85`
    val score = 85
    // Branch on the value of `score`
    when (score) {
        in 90..100 -> println("Grade A")
        in 80..89 -> println("Grade B")
        in 70..79 -> println("Grade C")
        else -> println("Grade F")
    }
}

when as an Expression

when सीधे एक value produce कर सकता है, जिसमें हर branch का result उस branch के match होने पर overall value बन जाता है। एक else branch चाहिए जब तक compiler यह साबित न कर सके कि हर case cover हो गया है।

Note: बिना subject वाला when { } हर branch को खुद एक boolean condition होने देता है।

उदाहरण: when as an Expression

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `number`, set to `4`
    val number = 4
    // Declare a read-only variable `description`; its value comes from the block below
    val description = when {
        number % 2 == 0 -> "even"
        else -> "odd"
    }
    // Print "$number is $description" to the console, with a trailing newline
    println("$number is $description")
}
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. किसी ऐसे type पर expression के रूप में when का उपयोग करते समय else branch भूल जाना जो exhaustive नहीं है, जिससे compile error आता है।
  2. यह न समझना कि when ranges, कई values, और यहां तक कि types भी match कर सकता है, और इसके बजाय if/else if की एक लंबी chain लिखना।
  3. बिना argument वाले when में overlapping conditions लिखना, यह न समझते हुए कि सिर्फ पहली matching branch चलती है।
चैप्टर सारांश
  • when किसी value को कई branches से compare करता है और जो पहला मेल खाए उसे चलाता है (या evaluate करता है)।
  • एक single branch commas से अलग की गई कई values, या in का उपयोग करके एक range match कर सकती है।
  • expression के रूप में उपयोग होने पर, when को आमतौर पर एक else branch चाहिए जब तक compiler यह साबित न कर सके कि सभी cases cover हैं (जैसे sealed class या Boolean)।
  • when को बिल्कुल बिना argument के भी उपयोग किया जा सकता है, conditions की एक साफ़ chain की तरह काम करते हुए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.