Difference between revisions of "KOTLIN: When Expression"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_when_expression.htm Consider a situation when you have large number of conditions to check. Though you can use if..else i...")
 
Line 6: Line 6:
  
 
Kotlin when expression is similar to the switch statement in C, C++ and Java.
 
Kotlin when expression is similar to the switch statement in C, C++ and Java.
Example
 
fun main(args: Array<String>) {
 
  val day = 2
 
  
  val result = when (day) {
+
===Example===
    1 -> "Monday"
+
 
    2 -> "Tuesday"
+
fun main(args: Array<String>) {
    3 -> "Wednesday"
+
    val day = 2
    4 -> "Thursday"
+
    5 -> "Friday"
+
    val result = when (day) {
    6 -> "Saturday"
+
      1 -> "Monday"
    7 -> "Sunday"
+
      2 -> "Tuesday"
    else -> "Invalid day."
+
      3 -> "Wednesday"
  }
+
      4 -> "Thursday"
  println(result)
+
      5 -> "Friday"
}
+
      6 -> "Saturday"
 +
      7 -> "Sunday"
 +
      else -> "Invalid day."
 +
    }
 +
    println(result)
 +
}
 +
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Tuesday
+
Tuesday
Kotlin when as Statement
+
 
 +
==Kotlin when as Statement==
 +
 
 
Kotlin when can be used either as an expression or as a statement, simply like a switch statement in Java. If it is used as an expression, the value of the first matching branch becomes the value of the overall expression.
 
Kotlin when can be used either as an expression or as a statement, simply like a switch statement in Java. If it is used as an expression, the value of the first matching branch becomes the value of the overall expression.
  
Example
+
===Example===
 +
 
 
Let's write above example once again without using expression form:
 
Let's write above example once again without using expression form:
  
fun main(args: Array<String>) {
+
fun main(args: Array<String>) {
  val day = 2
+
    val day = 2  
 +
 +
    when (day) {
 +
      1 -> println("Monday")
 +
      2 -> println("Tuesday")
 +
      3 -> println("Wednesday")
 +
      4 -> println("Thursday")
 +
      5 -> println("Friday")
 +
      6 -> println("Saturday")
 +
      7 -> println("Sunday")
 +
      else -> println("Invalid day.")
 +
    }
 +
}
  
  when (day) {
 
    1 -> println("Monday")
 
    2 -> println("Tuesday")
 
    3 -> println("Wednesday")
 
    4 -> println("Thursday")
 
    5 -> println("Friday")
 
    6 -> println("Saturday")
 
    7 -> println("Sunday")
 
    else -> println("Invalid day.")
 
  }
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Tuesday
+
Tuesday
Combine when Conditions
+
 
 +
==Combine when Conditions==
 +
 
 
We can combine multiple when conditions into a single condition.
 
We can combine multiple when conditions into a single condition.
  
Example
+
===Example===
fun main(args: Array<String>) {
+
 
  val day = 2
+
fun main(args: Array<String>) {
 +
    val day = 2
 +
 +
    when (day) {
 +
      1, 2, 3, 4, 5 -> println("Weekday")
 +
      else -> println("Weekend")
 +
    }
 +
}
  
  when (day) {
 
    1, 2, 3, 4, 5 -> println("Weekday")
 
    else -> println("Weekend")
 
  }
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Weekday
+
Weekday
Range in when Conditions
+
 
 +
==Range in when Conditions==
 +
 
 
Kotlin ranges are created using double dots .. and we can use them while checking when condition with the help of in operator.
 
Kotlin ranges are created using double dots .. and we can use them while checking when condition with the help of in operator.
  
Example
+
===Example===
fun main(args: Array<String>) {
+
 
  val day = 2
+
fun main(args: Array<String>) {
 +
    val day = 2
 +
 +
    when (day) {
 +
      in 1..5 -> println("Weekday")
 +
      else -> println("Weekend")
 +
    }
 +
}
  
  when (day) {
 
    in 1..5 -> println("Weekday")
 
    else -> println("Weekend")
 
  }
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Weekday
+
Weekday
Expression in when Conditions
+
 
 +
==Expression in when Conditions==
 +
 
 
Kotlin when can use arbitrary expressions instead of a constant as branch condition.
 
Kotlin when can use arbitrary expressions instead of a constant as branch condition.
  
Example
+
===Example===
fun main(args: Array<String>) {
+
 
  val x = 20
+
fun main(args: Array<String>) {
  val y = 10
+
    val x = 20
  val z = 10
+
    val y = 10
 
+
    val z = 10
  when (x) {
+
   
      (y+z) -> print("y + z = x = $x")
+
    when (x) {
      else -> print("Condition is not satisfied")
+
      (y+z) -> print("y + z = x = $x")
  }
+
      else -> print("Condition is not satisfied")
}
+
    }
 +
}
 +
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
y + z = x = 20
+
y + z = x = 20
Kotlin when with block of code
+
 
 +
==Kotlin when with block of code==
 +
 
 
Kotlin when braches can be put as block of code enclosed within curly braces.
 
Kotlin when braches can be put as block of code enclosed within curly braces.
  
Example
+
===Example===
fun main(args: Array<String>) {
+
 
  val day = 2
+
fun main(args: Array<String>) {
 +
    val day = 2  
 +
 +
    when (day) {
 +
      1 -> {
 +
        println("First day of the week")
 +
        println("Monday")
 +
      }
 +
      2 -> {
 +
        println("Second day of the week")
 +
        println("Tuesday")
 +
      }
 +
      3 -> {
 +
        println("Third day of the week")
 +
        println("Wednesday")
 +
      }
 +
      4 -> println("Thursday")
 +
      5 -> println("Friday")
 +
      6 -> println("Saturday")
 +
      7 -> println("Sunday")
 +
      else -> println("Invalid day.")
 +
    }
 +
}
  
  when (day) {
 
    1 -> {
 
        println("First day of the week")
 
        println("Monday")
 
    }
 
    2 -> {
 
        println("Second day of the week")
 
        println("Tuesday")
 
    }
 
    3 -> {
 
        println("Third day of the week")
 
        println("Wednesday")
 
    }
 
    4 -> println("Thursday")
 
    5 -> println("Friday")
 
    6 -> println("Saturday")
 
    7 -> println("Sunday")
 
    else -> println("Invalid day.")
 
  }
 
}
 
 
When you run the above Kotlin program, it will generate the following output:
 
When you run the above Kotlin program, it will generate the following output:
  
Second day of the week
+
Second day of the week
Tuesday
+
Tuesday
Quiz Time (Interview & Exams Preparation)
+
 
 +
==Quiz Time (Interview & Exams Preparation)==
 +
 
 
Q 1 - Which of the following is true about Kotlin when expression?
 
Q 1 - Which of the following is true about Kotlin when expression?
  

Revision as of 09:59, 22 July 2022

Sumber: https://www.tutorialspoint.com/kotlin/kotlin_when_expression.htm

Consider a situation when you have large number of conditions to check. Though you can use if..else if expression to handle the situation, but Kotlin provides when expression to handle the situation in nicer way. Using when expression is far easy and more clean in comparison to writing many if...else if expressions. Kotlin when expression evaluates a section of code among many alternatives as explained in below example.

Kotlin when matches its argument against all branches sequentially until some branch condition is satisfied.

Kotlin when expression is similar to the switch statement in C, C++ and Java.

Example

fun main(args: Array<String>) {
   val day = 2

   val result = when (day) {
     1 -> "Monday"
     2 -> "Tuesday"
     3 -> "Wednesday"
     4 -> "Thursday"
     5 -> "Friday"
     6 -> "Saturday"
     7 -> "Sunday"
     else -> "Invalid day."
   }
   println(result)
}

When you run the above Kotlin program, it will generate the following output:

Tuesday

Kotlin when as Statement

Kotlin when can be used either as an expression or as a statement, simply like a switch statement in Java. If it is used as an expression, the value of the first matching branch becomes the value of the overall expression.

Example

Let's write above example once again without using expression form:

fun main(args: Array<String>) {
   val day = 2 

   when (day) {
     1 -> println("Monday")
     2 -> println("Tuesday")
     3 -> println("Wednesday")
     4 -> println("Thursday")
     5 -> println("Friday")
     6 -> println("Saturday")
     7 -> println("Sunday")
     else -> println("Invalid day.")
   }
}

When you run the above Kotlin program, it will generate the following output:

Tuesday

Combine when Conditions

We can combine multiple when conditions into a single condition.

Example

fun main(args: Array<String>) {
   val day = 2

   when (day) {
     1, 2, 3, 4, 5 -> println("Weekday")
     else -> println("Weekend")
   }
}

When you run the above Kotlin program, it will generate the following output:

Weekday

Range in when Conditions

Kotlin ranges are created using double dots .. and we can use them while checking when condition with the help of in operator.

Example

fun main(args: Array<String>) {
   val day = 2

   when (day) {
     in 1..5 -> println("Weekday")
     else -> println("Weekend")
   }
}

When you run the above Kotlin program, it will generate the following output:

Weekday

Expression in when Conditions

Kotlin when can use arbitrary expressions instead of a constant as branch condition.

Example

fun main(args: Array<String>) {
   val x = 20
   val y = 10
   val z = 10
   
   when (x) {
      (y+z) -> print("y + z = x = $x")
      else -> print("Condition is not satisfied")
   }
}

When you run the above Kotlin program, it will generate the following output:

y + z = x = 20

Kotlin when with block of code

Kotlin when braches can be put as block of code enclosed within curly braces.

Example

fun main(args: Array<String>) {
   val day = 2 

   when (day) {
     1 -> {
        println("First day of the week")
        println("Monday")
     }
     2 -> {
        println("Second day of the week")
        println("Tuesday")
     }
     3 -> {
        println("Third day of the week")
        println("Wednesday")
     }
     4 -> println("Thursday")
     5 -> println("Friday")
     6 -> println("Saturday")
     7 -> println("Sunday")
     else -> println("Invalid day.")
   }
}

When you run the above Kotlin program, it will generate the following output:

Second day of the week
Tuesday

Quiz Time (Interview & Exams Preparation)

Q 1 - Which of the following is true about Kotlin when expression?

A - It is used to compare a single value against multiple conditions

B - Kotlin when expression can be used in place of if..else if expression

C - Kotlin when branches can be integer, string, array or ranges

D - All of the above

Q 2 - Kotlin when can be used as an expression as well as a statement?

A - True

B - False

Q 3 - Kotlin when is inspired by which of the following Java statement

A - switch statement

B - if statement

C - do...while statement

C - None of the above


Referensi