When

When

When

  • In Kotlin, when replaces the switch operator of other languages like Java.
  • A certain block of code needs to be executed when some condition is fulfilled.
  • The argument of when expression compares with all the branches one by one until some match is found. After the first match is found, it reaches to end of the when block and executes the code next to the when block. Unlike switch cases in java or any other programming language, we do not require a break statement at the end of each case.

In Kotlin, when can be used in two ways:

  1. when as a statement
  1. when as an expression
 
  1. AS A Statement.
fun main (args : Array<String>) { print("Enter the name of heavenly body: ") var name= readLine()!!.toString() when(name) { "Sun" -> print("-> Sun is a Star") "Moon" -> print("-> Moon is a Satellite") "Earth" -> print("-> Earth is a planet") else -> print("-> I don't know anything about it") } } //Output: //Enter the name of heavenly body: Sun //-> Sun is a Star //Enter the name of heavenly body: Mars //-> I don't know anything about it
 
  1. As A Expression.
  • If it is used as an expression, the value of the branch with which condition satisfied will be the value of overall expression.
  • As an expression when returns a value with which the argument matches and we can store it in a variable or print directly.
fun main(args : Array<String>) { print("Enter number of the Month: ") var monthOfYear = readLine()!!.toInt() var month= when(monthOfYear) { 1->"January" 2->"February" 3->"March" 4->"April" 5->"May" 6->"June" 7->"July" 8->"August" 9->"September" 10->"October" 11->"November" 12->"December" else-> "Not a month of year" } print(month) } // Output: //Enter number of the Month: 8 //August
 
  1. Combine multiple branches in one using comma:
fun main (args :Array<String>) { print("Enter name of the planet: ") var name=readLine()!!.toString() when(name) { "Mercury","Earth","Mars","Jupiter" ,"Neptune","Saturn","Venus","Uranus" -> print("This is a planet") else -> print("This not a planet") } } /* Output: Enter name of the planet: Earth Planet */
 
  1. Check the input value in the range or not:
fun main (args:Array<String>) { print("Enter the month number of year: ") var num= readLine()!!.toInt() when(num) { in 1..3 -> print("Spring season") in 4..6 -> print("Summer season") in 7..8 -> print("Rainy season") in 9..10 -> print("Autumn season") in 11..12 -> print("Winter season") !in 1..12 -> print("Enter valid month of year") } } /* Output: Enter the month number of year: 5 It is summer season Enter the month number of year: 14 Enter valid month of year */
 
  1. Check given variable is of a certain type or not:
fun main(args: Array<String>) { var num: Any = "GeeksforGeeks" when(num){ is Int -> println("It is an Integer") is String -> println("It is a String") is Double -> println("It is a Double") } } // Output: // It is a String
 
  1. Using when as a replacement for an if-else-if chain:
fun isOdd(x: Int) = x % 2 != 0 fun isEven(x: Int) = x % 2 == 0 fun main(args: Array<String>) { var num = 8 when{ isOdd(num) ->println("Odd") isEven(num) -> println("Even") else -> println("Neither even nor odd") } } // Output: // Even
  1. Check that a string contains a particular prefix or suffix:
fun hasPrefix(company: Any) = when (company) { is String -> company.startsWith("GeeksforGeeks") else -> false } fun main(args: Array<String>) { var company = "GeeksforGeeks a computer science portal" var result = hasPrefix(company) if(result) { println("Yes, string started with GeeksforGeeks") } else { println("No, String does not started with GeeksforGeeks") } } /* Output: Yes, string started with GeeksforGeeks */