...

Mobile App Development - Kotlin

Back to Course

Lesson Description


Lession - #397 Kotlin If....Else


How would you write if-else in Kotlin?

Kotlin has the following conditionals:
  • Use if to specify a block of code to be executed, in the event that if specified condition is true.
  • Use if to specify a block of code to be executed, in the event that if same condition is false.
  • Use else if to specify another condition to test, in the event that if first condition is false.





    Let's see an example

    
    
    fun main(>
    { val x = 5 val y = 6 if (x > y>
    { println("x is greater than y">
    } else { println("y is greater than x">
    } }



    Output



    Try it here

    What is Kotlin DSL?
    The Navigation component provides a Kotlin-based domain-specific language, or DSL, that relies on Kotlin's type-safe builders. This API allows you to declaratively compose your graph in your Kotlin code, rather than inside an XML resource. This can be useful if you wish to build your app's navigation dynamically.

    Kotlin in Action :