when Expression क्या है
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
// 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")
}
}
Login to try C/C++/Java/PHP code in the editor
Matching Multiple Values
एक single branch commas से अलग करके एक साथ कई values match कर सकता है, जिन cases के लिए एक जैसा व्यवहार चाहिए उनके लिए दोहराई गई branches से बचते हुए।
उदाहरण: Matching Multiple Values
// 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")
}
}
Login to try C/C++/Java/PHP code in the editor
Matching Ranges
किसी when branch के अंदर in का उपयोग करना यह जांचता है कि value किसी range के भीतर आती है या नहीं, जो कई chained comparisons से कहीं ज़्यादा साफ़ है।
उदाहरण: Matching Ranges
// 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")
}
}
Login to try C/C++/Java/PHP code in the editor
when as an Expression
when सीधे एक value produce कर सकता है, जिसमें हर branch का result उस branch के match होने पर overall value बन जाता है। एक else branch चाहिए जब तक compiler यह साबित न कर सके कि हर case cover हो गया है।
when { } हर branch को खुद एक boolean condition होने देता है।उदाहरण: when as an Expression
// 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")
}
Login to try C/C++/Java/PHP code in the editor
- किसी ऐसे type पर expression के रूप में
whenका उपयोग करते समयelsebranch भूल जाना जो exhaustive नहीं है, जिससे compile error आता है। - यह न समझना कि
whenranges, कई values, और यहां तक कि types भी match कर सकता है, और इसके बजायif/else ifकी एक लंबी chain लिखना। - बिना argument वाले
whenमें overlapping conditions लिखना, यह न समझते हुए कि सिर्फ पहली matching branch चलती है।
whenकिसी value को कई branches से compare करता है और जो पहला मेल खाए उसे चलाता है (या evaluate करता है)।- एक single branch commas से अलग की गई कई values, या
inका उपयोग करके एक range match कर सकती है। - expression के रूप में उपयोग होने पर,
whenको आमतौर पर एकelsebranch चाहिए जब तक 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: