Expression के रूप में if
In this page:
val result = if (condition) valueA else valueB
val result = if (condition1) valueA
else if (condition2) valueB
else valueC
if as a Statement
if का परिचित रूप किसी condition के आधार पर एक या दूसरा block of code चलाता है, खुद कोई value produce किए बिना -- यह बहुत सी दूसरी languages में if के व्यवहार करने का तरीका है।
उदाहरण: if as a Statement
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `age`, set to `20`
val age = 20
// Check whether `age >= 18`
if (age >= 18) {
// Print "You are an adult" to the console, with a trailing newline
println("You are an adult")
// Otherwise, run this branch
} else {
// Print "You are a minor" to the console, with a trailing newline
println("You are a minor")
}
}
Login to try C/C++/Java/PHP code in the editor
if as an Expression
Kotlin if को सीधे एक value produce करने भी देता है, जहां हर branch में आखिरी expression result बन जाता है। यह आपको outcome को सीधे एक val को assign करने देता है।
if को expression की तरह उपयोग करना हर branch के अंदर सेट होने वाले एक var को declare करने से बचाता है।उदाहरण: if as an Expression
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `age`, set to `20`
val age = 20
// Declare a read-only variable `status`, set to `if (age >= 18) "adult" else "minor"`
val status = if (age >= 18) "adult" else "minor"
// Print "Status: $status" to the console, with a trailing newline
println("Status: $status")
}
Login to try C/C++/Java/PHP code in the editor
Multi-Branch if Expressions
else if को chain करना किसी if expression को दो से ज़्यादा outcomes cover करने देता है, जिसमें आखिरी matching branch की आखिरी line resulting value देती है।
उदाहरण: Multi-Branch if Expressions
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `score`, set to `85`
val score = 85
// Declare a read-only variable `grade`, set to `if (score >= 90) "A"`
val grade = if (score >= 90) "A"
else if (score >= 80) "B"
else if (score >= 70) "C"
else "F"
// Print "Grade: $grade" to the console, with a trailing newline
println("Grade: $grade")
}
Login to try C/C++/Java/PHP code in the editor
Block Bodies in if Expressions
किसी if expression की हर branch एक multi-line block हो सकती है; उस block के अंदर आखिरी statement उस branch द्वारा योगदान की गई value बन जाता है।
उदाहरण: Block Bodies in if Expressions
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `number`, set to `7`
val number = 7
// Declare a read-only variable `description`; its value comes from the block below
val description = if (number % 2 == 0) {
// Declare a read-only variable `kind`, set to `"even"`
val kind = "even"
"The number is $kind"
// Otherwise, run this branch
} else {
"The number is odd"
}
// Print `description` to the console, with a trailing newline
println(description)
}
Login to try C/C++/Java/PHP code in the editor
- पुरानी आदत से
ifको सिर्फ एक statement के रूप में लिखना, फिर expression के रूप में उपयोग करने के बजाय हर branch में अलग से एक variable assign करना। - यह भूल जाना कि expression के रूप में उपयोग किए
ifको एकelsebranch चाहिए, नहीं तो compiler हर case में value की गारंटी नहीं दे सकता। - किसी
ifexpression के अंदर side effects (जैसे printing) डालना जिसे एक साफ़ value भी return करनी है, concerns मिलाते हुए।
- Kotlin में,
ifको एक statement (यह चुनने के लिए कि कौन सा block चले) या एक expression (एक value produce करने के लिए) के रूप में उपयोग किया जा सकता है। - expression के रूप में उपयोग होने पर, हर branch की आखिरी line उस branch द्वारा produce की गई value है।
- एक
ifexpression को हमेशा value produce करने की गारंटी देने के लिए एकelsebranch चाहिए। ifको expression के रूप में उपयोग करना अक्सर एक अलग mutable variable की ज़रूरत हटा देता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: