Kotlin control flow with if and when

In this post we are going to understand how to create if / when for control flow in Kotlin

The “if” expression for control flow

In Kotlin an if sentence has this form

if(condition) {
   // some code under this condition 
}

We can also see sentences with else

if(condition) { 
  // some code under this condition    
} else { 
 // some code under this ´else´ condition 
}

Look at this example. Here we use several conditions.

var age = 30
if (age > 40) {
    println("Age is over 40")
} else if (age == 40) {
    println("Age is 40")
} else {
    println("Age is less than 40")
}

Unlike Java, if else in Kotlin can be used to return a value and assign this return to a variable.

See this example: Here we directly assign the result of multiplying the age by a value, according to each condition, to the risk variable.

fun main() {
    var age = 30
    var risk = if (age > 40) {
        println("Age is over 40")
        age * 0.10
    } else if (age == 40) {
        println("Age is 40")
        age * 0.9
    } else {
        println("Age is less than 40")
        age * 0.05
    }
    println("The result of risk is $risk")
}

Kotlin If

The “when” expression for control flow

If you come from Java, sure you know the “switch” expresión. Kotlin has a when expression in replace of “switch” but when is more powerful.

Look this example: We evaluate different values. In case none of them are met, the flow enters through else.

var age = 30
when (age) {
    30 -> println("age == 30")
    40 -> println("age == 40")
    else -> {
        // block code
        println("the age is $age")
    }
}

// output -> age == 30

Kotlin when

Passing multiple values to when expresion: We can evaluate several values simultaneously in the same condition.

var age = 5
when (age) {
    1,2,3,4,5 -> println("age is between 1 - 5")
    6,7,8,9,10 -> println("age is between 6 - 10")
    else -> println("the age is $age")
}

// output -> age is between 1 - 5

We can include ranges:

age = 5
when (age) {
    in 1..5 -> println("age is in the range 1..5")
    in 6..10 -> println("age is in the range 6..10")
    else -> println("the age is $age")
}

// output -> age is in the range 1..5

Using negatives is very simple in conjunction with ranges:

var age = 20
when (age) {
    in 1..5 -> println("age is in the range 1..5")
    in 6..10 -> println("age is in the range 6..10")
    !in 1..10 -> println("the age is $age")
}

// output -> the age is not in range 1..10

We can create quick functions using when

fun ageDescription (age: Int) = when(age) {
    in 1..18 -> "Minor"
    in 19..70-> "Adult"
    else-> "Retired"
}

fun main() {

    var age = 50
    println(ageDescription(age))

}

// output -> Adult

Conclusion

We did a quick review of the most important point about control flow using “if” and “when” in Kotlin. The biggest point is the ability to assign the return value directly to a variable. In addition, the inclusion of “when” is more efficient and can replace “if” many times.