When Expression and Its Usage in Kotlin


Definition

  • "when" replaces the switch statement in Java. It checks for a series of conditions. The else part is mandatory.
  • "when" expression assigns the value from the first satisfied condition to a variable.
  • This can be used with or without arguments.
  • In addition to constants, expressions can also be used as conditions.
  • When no condition is satisfied, the value in the else block is returned.

Types of when Usage in Kotlin

  • "when" -  Switching cases
  • "when" without argument - Replacement for if else if
  • Combined Cases in when
  • Combined Conditions in when
  • Arbitrary expressions as branch conditions
  • Ranging Cases within or !in
  • Check values with is or !is (Smart casting check)

Syntax

1. "when" -  Switching cases

when(variable_name){
	value1 -> //Block of code
	value2 -> //Block of code
	value3 -> //Block of code
	else ->//Block of code
}

For example,

var num = when(x){
	"/" -> a/b
	"*" -> a*b
	else -> 0
}

2. "when" without argument - Replacement for if else if

when{
	value1 -> //Block of code
	number1 -> //Block of code
	string1 -> //Block of code
	else -> //Block of code
}

For example,

var str = when {
    //Expressions can also be added as condition
	num == 5 -> "Five"
	else -> println("Invalid")
}

3. Combined Cases in when

when (number){
	number1, number2 -> //Block of code
	number3,number4 -> //Block of code
	else -> //Block of code
}

For example,
 
var result = when (number) {
    //Expressions can also be added as condition
	1,2 -> "One or Two"
	3,4 -> "three or Four"
	else -> "Invalid"
}

4. Combined Conditions in when

when {
	condition1 || condition2 -> //Block of code
	condition3 || condition4 -> //Block of code
	else -> //Block of code
}

For example,
 
var str = when {
    //Expressions can also be added as condition
	value.contains("#") || value.contains("*") -> "Hash or Asterick"
	value.contains("<") || value.contains(">") -> "Less than or Greater than"
	else -> println("Invalid")
}

5. Arbitrary expressions as branch conditions

when{
	condition1 -> //Block of code
	condition2 -> //Block of code
	condition3 -> //Block of code
	else -> //Block of code
}

For example,

var str = when{
    //Expressions can also be added as condition
	value.contains("#") -> "Hash"
	value.contains("*") -> "Asterick"
	num==5 -> "Five"
	else -> println("Invalid")
}

6. Ranging Cases with in or !in

when (num) {
    in range -> //Block of Code
    !in range -> //Block of Code
    else -> //Block of Code
}

For example,

var str = when (num) {
    in 100..200 -> "Between 100 and 200"
    !in 300..400 -> "Not between 300 and 400"
    else -> "INVALID"
}

7. Check values with is or !is (Smart casting check)

when (num) {
    is condition -> //Block of Code
    !is condition -> //Block of Code
    else -> //Block of Code
}

For example,

var str = when (value) {
    is String -> "The value is a String"
    else -> "INVALID"
}

When Expression Example Program in Kotlin

// When Expression Kotlin example program
// Conditional Statements Kotlin Programs, Basic Kotlin Programs
fun main(args: Array<String>) {
    var value1 = "QWERTY#"
    var num = 5

    var value = when{
        //This condition will not be satisfied
        value1.contains("*") -> "Asterick"
        //This condition will be satisfied and returned
        value1.contains("#") -> "Hash"
        //This condition will not be checked
        num.equals(5) -> "Five"
        else -> "Invalid"
    }

    //Printing the result from when expression
    println("The value from when expression is : $value")
}

Sample Output

The value from when expression is : Hash