Quick actions

cmd+k|ctrl+k

Navigation

Languages

When Expressions

Snippet info

Language

Kotlin

Visibility

public

Author

habib17

Created

2019-11-01T00:57:57Z

Updated

2019-11-01T00:57:57Z

fun main(args : Array<String>){
  cekWhenExpression()
}

fun likeIf(){
      val value = 7
    
    when(value){
        6 -> println("value is 6")
        7 -> println("value is 7")
        8 -> println("value is 8")
    }
}

fun likeIfElse(){
    val value = 20
    
    when(value){
        6 -> println("value is 6")
        7 -> println("value is 7")
        8 -> println("value is 8")
        else -> println("value cannot be reached")
    }
}

fun withCurlyBrances(){
    val value = 7
    val stringOfValue = when(value){
        6 -> {
            println("Six")
            "value is 6"
        }
        7 -> {
            println("Seven")
            "value is 7"
        }
        8 -> {
            println("Eight")
            "value is 8"
        }
        else -> {
            println("undefined")
            "value cannot be reached"
        }
    }
}

fun mengembalikanNilai(){
    val value = 7
    val stringOfValue = when(value){
        6 -> "value is 6"
        7 -> "value is 7"
        8 -> "value is 8"
        else -> "value cannot be reached"
    }
    
    println(stringOfValue)
}


fun cekJenis(){
    val anyType : Any = "horee"
    when(anyType){
        is Long -> println("the value has a Long type")
        is String -> println("the value has a String type")
        else -> println("undefined")
    }
}

fun cekRange(){
    val value = 30
    val ranges = 47..50
    
    when(value){
        in ranges -> println("value is in the range")
        !in ranges -> println("value is outside the range")
        else -> println("value undefined")
    }
}

fun cekWhenExpression(){
    val anyType: Any = 500.00
    when (anyType) {
    is Long -> println("the value has a Long type")
    is Int -> println("the value has a Int type")
    is Double -> println("the value has a Double type")
    else -> println("undefined")
}
}
INFO