Answers for "Kotlin when"

11

Kotlin when

val x = 3
when(x) {
    3 -> println("yes")
    8 -> println("no")
    else -> println("maybe")
}
// when can also be used as an expression!
val y = when(x) {
    3 -> "yes"
    8 -> "no"
    else -> "maybe"
}
println(y) // "yes"
Posted by: Guest on May-10-2020
7

when kotlin

when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> { // Note the block
        print("x is neither 1 nor 2")
    }
}
Posted by: Guest on April-25-2020
3

kotlin when

fun numberTypeName(x: Number) = when(x) {
 0 -> "Zero" // Equality check
 in 1..4 -> "Four or less" // Range check
 5, 6, 7 -> "Five to seven" // Multiple values
 is Byte -> "Byte" // Type check
 else -> "Some number"
}
Posted by: Guest on January-15-2021
2

kotlin when

fun signAsString(x: Int)= when {
 x < 0 -> "Negative"
 x == 0 -> "Zero"
 else -> "Positive"
}
Posted by: Guest on January-15-2021
0

when kotlin

#include<bits/stdc++.h>
using namespace std;
int main()
{
  cout<<"HEllo";
}
Posted by: Guest on July-14-2021
0

when kotlin

var greeting : String ? = null
fun main(){
	when(greeting){
		null -> println("Hi")
        else -> println(greeting)
        }
   }
Posted by: Guest on July-14-2021

Browse Popular Code Answers by Language