1val x = 3
2when(x) {
3 3 -> println("yes")
4 8 -> println("no")
5 else -> println("maybe")
6}
7// when can also be used as an expression!
8val y = when(x) {
9 3 -> "yes"
10 8 -> "no"
11 else -> "maybe"
12}
13println(y) // "yes"
1when (x) {
2 1 -> print("x == 1")
3 2 -> print("x == 2")
4 else -> { // Note the block
5 print("x is neither 1 nor 2")
6 }
7}
1fun numberTypeName(x: Number) = when(x) {
2 0 -> "Zero" // Equality check
3 in 1..4 -> "Four or less" // Range check
4 5, 6, 7 -> "Five to seven" // Multiple values
5 is Byte -> "Byte" // Type check
6 else -> "Some number"
7}
1fun signAsString(x: Int)= when {
2 x < 0 -> "Negative"
3 x == 0 -> "Zero"
4 else -> "Positive"
5}
1#include<bits/stdc++.h>
2using namespace std;
3int main()
4{
5 cout<<"HEllo";
6}
7
1var greeting : String ? = null
2fun main(){
3 when(greeting){
4 null -> println("Hi")
5 else -> println(greeting)
6 }
7 }