Kotlin IF-ELSE Condition & Usage Examples | Android Introduction #12
In this reading, you will learn how to use the conditional if, if-else and if-else-if statements.
If and if-else statements
You use the if statement to invoke conditional code. In other words, the code will only run if a predicate is fulfilled. For example, Steven from the story used in an earlier reading can go to the cinema if he finishes his homework:
fun main() {
val finishedHomework = true
if (finishedHomework) {
println("Can go to the cinema")
}
}
The additional else block is used to provide an alternative for the cases when a predicate is not fulfilled.
fun main() {
val finishedHomework = true
if (finishedHomework) {
println("Can go to the cinema")
} else {
println("Cannot go to the cinema")
}
}
Often, the predicate is an expression that compares two values. For instance, if a value is smaller or bigger than another value.
fun main() {
val i = 1 // or 5
if (i < 3) {
println("Smaller")
} else {
println("Bigger")
}
// Prints Smaller if i == 1, or Bigger if i == 5
}
Using if-else as expressions
An if-else statement can be used as an expression. In other words, to return a value that can be stored in a variable. The returned value is the value returned by the body block that was chosen. In the code below, the predicate returns true, so the first body is chosen, so 10.0 is returned.
fun main() {
val haveSomeExtraMoney = true
val tip: Double =
if (haveSomeExtraMoney) {
10.0
} else {
0.0
}
println(tip) // 10.0
}
Inside if-else bodies, you can include more than one statement. The value returned by a body is the last expression it has.
fun main() {
val haveSomeExtraMoney = true
val tip: Double =
if (haveSomeExtraMoney) {
println("Here you go") // Here you go
1.0 // this value is ignored, because it is not the last one
5.0 // this value is ignored, because it is not the last one
10.0
} else {
println("Sorry, I am broke")
0.0
}
println(tip) // 10.0
}
However, if you have only one expression in your body, you can skip braces.
fun main() {
val haveSomeExtraMoney = true
val tip: Double = if (haveSomeExtraMoney) 10.0 else 0.0
println(tip) // 10.0
}
If-else-if
By placing one if-else block after another, you form a structure known as if-else-if. It is a structure that checks conditions one after another until it finds the first one that is fulfilled, and it calls its body. If all the conditions return false, the else block is called. This means that in the following code, if probability is smaller or equal to 40, then only Unlikely will be printed. If probability is over 40 but smaller or equal to 80, then only Likely will be printed. If probability is over 80 but smaller or equal to 100, then only Yes will be printed. Otherwise, What? will be printed.
fun main() {
println("Is it going to rain?")
val probability = 70
if (probability <= 40) {
println("Unlikely")
} else if (probability <= 80) {
println("Likely")
} else if (probability < 100) {
println("Yes")
} else {
println("What?")
}
}
If-else-if is a popular structure in many languages but not in Kotlin. This is because there is a better alternative, called the when statement, but it’s good to know how to use if-else-if in case you ever need it.
In this reading, you learned how to use the conditionals if, if-else, and if-else-if statements.