Kotlin's control flow is the same as in other languages, order, branching, looping
There are two types of branching, if-else and when (similar to switch in Java).
If statement:
Operation Result:
The case statement in Java not only supports integer values, the latest JVM also supports strings, kotlin when statements are more powerful, switch variables can even be various types such as Double,array, in other words when statements are more powerful than switch statements, The ability to if-else statements is already comparable:
Operation Result:
While and Do-while statements
Not much different from Java
Operation Result:
For loop, use range as the For loop control rule:
[Email protected] and [email protected] similar to the label labels in Java, can be used for break and continue statements, the label can appear almost anywhere
Operation Result:
Simplified flow of control;
There are other uses in Kotlin:
The If-else statement and the When statement can appear in a separate statement to initialize a variable.
Operation Result:
This usage can be used instead of ternary expressions,
Kotlin also has ternary expressions, but unlike in Java, ternary expressions in Kotlin are used in null control and are now not discussed.
Kotlin Tutorial Learning-Control Flow