Branch statements
Kotlin has two types of branch statements: if… The else and the when.
If expression
Kotlin can use if… as Java does. Else statement that modifies the value of a variable by judging the condition:
var name = ""
if (local == "en") {
name = "lqr"
} else {
name = Daniel Wu
}
Copy the code
But that’s not neat enough. Kotlin’s answer to if… Else support if expression, can simplify the above code:
val name = if (local == "en") "lqr" else Daniel Wu
Copy the code
There are no ternary operators in Kotlin, but they can be handled using if expressions.
When the expression
When in Kotlin is the equivalent of switch in Java. The when statement is used to handle multi-branch conditional judgments, and the code is clearer:
val x = 5
when (x) {
5 -> println("x is 5")
is Int -> println("Hello $x")
in 1.100. -> println("$xis in 1.. 100")!in 1.100. -> println("$xis not in 1.. 100")
args[0].toInt() -> println("x == args[0]")}Copy the code
As you can see, the conditional branch of WHEN is very powerful, supporting not only value judgments but even expression judgments. Like if expressions, Kotlin supports when expressions:
val hero = when (local) {
"en" -> "Hero"
"zh" -> "Hero"
"fr" -> "The Un heros"
"de" -> "Held"
else -> "Unknown"
}
println(hero)
Copy the code
Kotlin’s when is more powerful than Java’s switch:
- Enhanced switch, support any type
- Support for pure expression conditional branching (similar to if)
When the expression
To be complete, you have to list all the cases
Looping statements
There are basically two types of loop statements in Kotlin: for and while.
The for loop
For (element in elements)…
for (arg in args) {
println(arg)
}
Copy the code
Iterator (), and then the iterator’s next() and hasNext() to process the elements in the array. So the key point of the loop is iterator.
The for loop in Java is also an iterator, but Java’s arrays need to implement the Iterable
interface and implement iterator(), whereas Kotlin’s classes override operator Fun iterator() for any class.
Array supports fetching index values while in a for loop:
// index for loop
for ((index, value) in args.withIndex()) {
println("$index : $value")}// This is equivalent to the following
for (indexValue in args.withIndex()) {
println("${indexValue.index} : ${indexValue.value}")}Copy the code
Array withIndex() returns Iterable
>, where IndexedValue is a data class and can be extracted using () :
public data class IndexedValue<out T>(public val index: Int.public val value: T)
Copy the code
The while loop
In addition to the for loop, Kotlin also supports the while loop, which has two formats. The differences between the two are as follows:
- While: Judge and then execute
- Do-while: execute first and judge later
var x = 5
while (x > 0) {
print("$x ") // 5, 4, 3, 2, 1
x--
}
var x = 5
do {
print("$x ") // 5, 4, 3, 2, 1
x--
} while (x > 0)
Copy the code
Skip and terminate loops
Kotlin also supports instructions to skip and terminate loop statements, respectively:
continue
: Skips the current loopbreak
: terminates the current loop
val args = intArrayOf(1.2.3.4.5)
for (arg in args) {
if (arg == 2) continue
if (arg == 4) break
print("$arg ") / / 1 3
}
Copy the code
A return is also commonly used in development to terminate a loop, but unlike break, a return actually terminates the execution of a method.
When you have multiple layers of loop nesting and need to skip or terminate the inner loop statement depending on the condition, you can use the tag:
val args = intArrayOf(1.2.3.4.5)
Outter@ for (arg in args) {
println("outter: $arg")
var i = arg
print("inner: ")
Inner@ while (i > 0) {
if (arg == 2) break@Inner // When the loop element is 2, skip while and proceed to the next for
if (arg == 4) break@Outter // If the loop element is 3, jump out for
print("$i ")
i--
}
println()
}
Copy the code