Kotlin Learning: Control Flow as Expressions

Control flow in kotlin can be used as an expression unlike other languages:

Code:

1.

fun main(args: Array) {

val s=”mmmmmmmkotlin”.endsWith(“kotlin1”,true);
print(s)
}
output:false

2.if_else
>

val s=if(1>2)false else true

3.try catch

try
{
val s=”nihao”
val ss=s.toint();
true;
}catch{
false
}’

Provision: The last line of the expression must be the return value;
4: when in kotlin there is no switch, the effect is equivalent to switch, but when is an omnipotent switch is limited

when(1)
{
1->1
else ->0
}
The above writing method can also use no parameter when

when{
1>1 ->1
else ->0
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324882907&siteId=291194637