If expression
The branch of if can be a block of code, and the final expression is the value of that block.
val maxValue = if (20 > 10) {
println("maxValue is 20")
20
} else {
println("maxValue is 10")
10
}
println(maxValue) //20
The following code shows the return value of If
val list = listOf(1, 4, 10, 4, 10, 30)
val value = if (list.size > 0) list.size else null
println(value) //6
If the branch of an If expression is used to execute a command, then the return value type at this time is Unit, and the If statement at this time looks the same as that in Java.
val value1 = if (list.size > 0) println("1") else println("2")
println(value1.javaClass) //class kotlin.Unit
If you treat if as an expression rather than a statement (for example, returning its value or assigning it to a variable), the expression requires an else branch.
When expression
when expressions are similar to switch/case in Java, but much more powerful. when can be used either as an expression or as a statement. when compares parameters with all branch conditions sequentially until a branch satisfies the condition, then it runs the expression on the right.
If when is used as an expression, the value of a qualified branch is the value of the entire expression, and if used as a statement, the value of an individual branch is ignored. Unlike Java's switch/case, when expressions can have parameters of any type, and branching can also be a condition.
Like if, each branch of When can be a block of code whose value is the value of the last expression in the block of code. If the other branches do not satisfy the condition, they will be evaluated in the else branch.
If when is used as an expression, there must be an else branch unless the compiler can detect that all possible cases have been overwritten.
If many branches need to be handled in the same way, you can put multiple branch conditions together, separated by commas.
val value = 2
when (value) {
in 4..9 -> println("in 4..9") //Interval judgement
3 -> println("value is 3") //Equivalence Judgment
2, 6 -> println("value is 2 or 6") //Multivalue Equivalence Judgment
is Int -> println("is Int") //Type judgement
else -> println("else") //If none of the above conditions are met, else is executed
}
fun main(args: Array<String>) {
fun parser(obj: Any): String =
when (obj) {
1 -> "value is 1"
"4" -> "value is string 4"
is Long -> "value type is long"
else -> "unknown"
}
println(parser(1))
println(parser(1L))
println(parser("4"))
println(parser(100L))
println(parser(100.0))
}
value is 1
value type is long
value is string 4
value type is long
unknown
for cycle
The most similar form of for loop in Java is
val list = listOf(1, 4, 10, 34, 10)
for (value in list) {
println(value)
}
Traverse by index
val items = listOf("H", "e", "l", "l", "o")
//Traversing List s by Index
for (index in items.indices) {
println("${index}The corresponding values are:${items[index]}")
}
You can also get the current index and corresponding values in each loop
val list = listOf(1, 4, 10, 34, 10)
for ((index, value) in list.withIndex()) {
println("index : $index , value :$value")
}
You can also customize the loop interval
for (index in 2..10) {
println(index)
}
While and do/while loops
While and do/while are not very different from Java
val list = listOf(1, 4, 15, 2, 4, 10, 0, 9)
var index = 0
while (index < list.size) {
println(list[index])
index++
}
val list = listOf(1, 4, 15, 2, 4, 10, 0, 9)
var index = 0
do {
println(list[index])
index++
} while (index < list.size)
Ranges
The Ranges expression uses a... operator to declare a closed interval, which is defined to implement a RangTo method.
The following two forms of declaration are equivalent
var index = 5
if (index >= 0 && index <= 10) {
}
if (index in 0..10) {
}
When digital ranges are iterated, the compiler optimizes them by converting them to the same bytecode as the for loop using index in Java
Ranges grow by default, so code like the following will not be executed
for (index in 10..0) {
println(index)
}
You can use the downTo function instead to reduce it.
for (index in 10 downTo 0) {
println(index)
}
step can be used in ranges to define the length of each cycle increment or increment:
for (index in 1..8 step 2){
println(index)
}
for (index in 8 downTo 1 step 2) {
println(index)
}
The above statements are all closed intervals. If you want to declare an open interval, you can use the until function:
for (index in 0 until 4){
println(index)
}