If expression
In Kotlin, an if statement is an expression that returns a value. There are several uses:
A / / usage
var min = a
if (a > b) min = b // omit the curly braces
2 / / usage
var min: Int
if (a > b) {
min = b
} else {
min = a
}
Copy the code
The above is used as a statement. The following is used as an expression:
A / / usage
var min = if (a < b) a else b
2 / / usage
var min = if (a < b) { // If branches can be blocks of code, with the final expression as the value of that block
print(a)
a
} else {
print(b)
b
} //a and b are assigned to min as return values
Copy the code
Note that when an if statement is used as an expression (either to return its value or to assign it to a variable), it must have an else branch, otherwise an error will be reported.
When expression
The WHEN expression in Kotlin is similar to the Switch /case expression in Java, but it supports more types and many new features.
// Common usage
var x: Int = 3
when (x) {
1 -> print("one")
2 -> print("two")
3 -> print("three")
else -> print("zero")}// The output is: three, if all criteria are not met, then the else branch is used, printing: zero.
Copy the code
In the following example, the last expression x in the else block represents the result returned. If x = 3, “three” is printed, but no value is returned, and b is printed: Kotlin.Unit, interested friends can try ~
// Contains the usage of code blocks and return values
var x: Int = 0
val b = when (x) {
1 -> print("one")
2 -> print("two")
3.4 -> print("three") // The same branch can have multiple values, separated by commas
else -> {
print("zero")
x
}
}
print("Print:$b") //// The command output is zero 0
Copy the code
You can use in and! In to check whether a value is in the set interval:
when (x) {
in 1.20. -> print("x is in the range")!in 20.30. -> print("x is outside the range")
else -> print("none of the above")}Copy the code
You can also use is and! Is to determine if a value is of a specific type:
when (view) {
is TextView -> view.text // Automatic type conversion
is ListView -> view.getItemAtPosition(1)!is ImageView-> print("this is not ImageView")
else -> print("no this view")}Copy the code
The is operator in Kotlin can detect immutable values and convert them intelligently. for example, if the is TextView is valid, the view will be automatically converted to TextView, and the TextView method can be directly called to get the text value.
While expression
Kotlin’s while expression and do… The while expression is used just as in Java
/ / while usage
var x: Int = 5
while (x > 0) {
x--
}
// do... While usage
var x: Int = 5
do {
x--
} while (x > 0)
Copy the code
4, for expression
Iterate over a number set in Kotlin
val array: Array<String> = arrayOf("1"."2"."3")
for (item in array) {
println("Print:$item") // Output: 1, 2, 3
}
Copy the code
Print array subscripts in kotlin using array.indices
val array: Array<String> = arrayOf("1"."2"."3")
for (index in array.indices) {
println("Print:$index") // The output is 0, 1, 2
}
Copy the code
In Kotlin, we print both the index and the value of the array, array.withindex (), index before the index and value after the value
val array: Array<String> = arrayOf("1"."2"."3")
for ((index, value) in array.withIndex()) {
println("(the index =$index---value=$value)") If (index=0-- value=1), (index=1-- value=2), (index=2-- value=3)
}
Copy the code
Print 1 to 100 in Kotlin
for (item in 1.100.) {
println("Print:$item") // Output: 1, 2, 3, 4... 100
}
Copy the code
If you want to print in reverse order 100 to 1, use the downTo method
for (item in 100 downTo 1) {
println("Print:$item") // Output: 100 99 98 97... 1
}
Copy the code
If you want to print some numbers regularly from 1 to 100, use the step method
for (item in 1.100. step 2) {
println("Print:$item") // Output: 1 3 5 7 9... 99
}
Copy the code
Summary: The simple usage of kotlin control flows if, when, for, and while is described above. They are written in the same way as Java, but provide more methods for development and reduce the amount of code.