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

Expression के रूप में if

Kotlin में, एक if सिर्फ यह नहीं चुन सकता कि कौन सा code चले, यह सीधे एक value भी वापस दे सकता है, जैसे किसी सवाल का जवाब देना।
Syntax
kotlin
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

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

if as an Expression

Kotlin if को सीधे एक value produce करने भी देता है, जहां हर branch में आखिरी expression result बन जाता है। यह आपको outcome को सीधे एक val को assign करने देता है।

Note: if को expression की तरह उपयोग करना हर branch के अंदर सेट होने वाले एक var को declare करने से बचाता है।

उदाहरण: if as an Expression

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

Multi-Branch if Expressions

else if को chain करना किसी if expression को दो से ज़्यादा outcomes cover करने देता है, जिसमें आखिरी matching branch की आखिरी line resulting value देती है।

उदाहरण: Multi-Branch if Expressions

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

Block Bodies in if Expressions

किसी if expression की हर branch एक multi-line block हो सकती है; उस block के अंदर आखिरी statement उस branch द्वारा योगदान की गई value बन जाता है।

उदाहरण: Block Bodies in if Expressions

markup
// 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)
}
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. पुरानी आदत से if को सिर्फ एक statement के रूप में लिखना, फिर expression के रूप में उपयोग करने के बजाय हर branch में अलग से एक variable assign करना।
  2. यह भूल जाना कि expression के रूप में उपयोग किए if को एक else branch चाहिए, नहीं तो compiler हर case में value की गारंटी नहीं दे सकता।
  3. किसी if expression के अंदर side effects (जैसे printing) डालना जिसे एक साफ़ value भी return करनी है, concerns मिलाते हुए।
चैप्टर सारांश
  • Kotlin में, if को एक statement (यह चुनने के लिए कि कौन सा block चले) या एक expression (एक value produce करने के लिए) के रूप में उपयोग किया जा सकता है।
  • expression के रूप में उपयोग होने पर, हर branch की आखिरी line उस branch द्वारा produce की गई value है।
  • एक if expression को हमेशा value produce करने की गारंटी देने के लिए एक else branch चाहिए।
  • if को expression के रूप में उपयोग करना अक्सर एक अलग mutable variable की ज़रूरत हटा देता है।
🔒

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.