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.
Kotlin Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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") } |
Output:
1 2 3 |
Max = 5 |