Control Flow - Kotlin

Watch this here


if..else :

 If we want to check some condition then, we use if..else. If the condition which we are giving is true then it will execute set of code, otherwise it will execute different set of code. Here the else part is not mandatory.

                
    val age = 10
    if (age < 18) {
        println("Not eligible for voting")
    }else{
        println("Eligible for voting")
    }
 
                

when :

  "when" executes set of codes from multiple condition. Its like using if..else inside other if..else statements.

                
        val age = 10
        when(age){
            in 0..17 ->{
                println("Not eligible for voting")
            }
            in 18..60 ->{
                println("Eligible for voting")
                println("Not a senior citizen")
            }
            else ->{
                println("Eligible for voting")
                println("Is a senior citizen")
            }
        }
 
                

12800 Subscribers