Kotlin Conditional Control
IF Expression
An if statement contains a Boolean expression and one or more statements.
// Traditional usage
var max = a
if (a < b) max = b
// Using else
var max: Int
if (a > b) {
max = a
} else {
max = b
}
// As an expression
val max = if (a > b) a else b
We can also assign the result of an IF expression to a variable.
val max = if (a > b) {
print("Choose a")
a
} else {
print("Choose b")
b
}
This also indicates that I don't need a ternary operator like Java because we can use it to simply implement:
val c = if (condition) a else b
Example
fun main(args: Array<String>) {
var x = 0
if(x>0){
println("x is greater than 0")
} else if (x == 0) {
println("x is equal to 0")
} else {
println("x is less than 0")
}
var a = 1
var b = 2
val c = if (a >= b) a else b
println("The value of c is $c")
}
Output:
x is equal to 0
The value of c is 2
Using Ranges
Use the in operator to check if a number is within a specified range, with the range format x..y
:
Example
fun main(args: Array<String>) {
val x = 5
val y = 9
if (x in 1..8) {
println("x is within the range")
}
}
Output:
x is within the range
When Expression
When compares its argument to all the branch conditions in order until a branch condition is met.
When can be used both as an expression and as a statement. If used as an expression, the value of the branch that meets the condition is the value of the entire expression. If used as a statement, the value of individual branches is ignored.
When is similar to the switch operator in other languages. Its simplest form is as follows:
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note this block
print("x is neither 1 nor 2")
}
}
In when, else is the same as default in switch. If none of the other branches meet the condition, the else branch will be evaluated.
If many branches need to be handled in the same way, multiple branch conditions can be grouped together, separated by commas:
when (x) {
0, 1 -> print("x == 0 or x == 1")
else -> print("otherwise")
}
We can also check whether a value is in (in) or not in (!in) a range or set:
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
Another possibility is to check whether a value is (is) or is not (!is) a specific type of value. Note: Due to smart casting, you can access methods and properties of the type without any additional checks.
fun hasPrefix(x: Any) = when(x) {
is String -> x.startsWith("prefix")
else -> false
}
When can also be used to replace if-else if chains. If no argument is provided, all branch conditions are simply boolean expressions, and the branch is executed when a condition is true:
when {
x.isOdd() -> print("x is odd")
x.isEven() -> print("x is even")
else -> print("x is funny")
}
Example
``` fun main(args: Array<String>) { var x = 0 when (x) { 0, 1 -> println("x == 0 or x == 1") else -> println("otherwise") }
when (x) {
1 -> println("x == 1")
2 -> println("x == 2")
else -> { // Note this block
println("x is neither 1 nor 2")
}
}