Kotlin Nested If
Chapter:
Kotlin
Last Updated:
18-11-2017 06:22:28 UTC
Program:
/* ............... START ............... */
fun main(args: Array<String>) {
val number1 = 3
val number2 = 5
val number3 = -2
val max = if (number1 > number2) {
if (number1 > number3)
number1
else
number3
} else {
if (number2 > number3)
number2
else
number3
}
println("Max = $max")
}
/* ............... END ............... */
Output
Notes:
-
A nested if statement is an if-else statement with another if statement as the if body or the else body.
- Evaluates the condition of the outer if. If it evaluates to false, don't run the code in the if body.
- In Nested if in kotlin basically evaluates the outer condition, and only when it succeeds does it evaluate the inner condition.