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

Max = 5

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.
Similar Programs Chapter Last Updated
Kotlin Continue Kotlin 02-03-2018
Kotlin Break Statement Kotlin 27-01-2018
Kotlin For Loop Kotlin 30-11-2017
Kotlin Do While Loop Kotlin 19-11-2017
Kotlin While Loop Kotlin 19-11-2017
Kotlin When Statement Kotlin 18-11-2017
Kotlin If Else If Statement Kotlin 22-09-2018
Kotlin If Statement Kotlin 17-11-2017
Kotlin Print And Println Kotlin 22-09-2018
Kotlin Type Conversion Kotlin 17-11-2017
Kotlin Logical Operators Kotlin 17-11-2017
Kotlin Assignment Operators Kotlin 15-11-2017
Kotlin Arithmetic Operators Kotlin 15-11-2017
Kotlin Data Types Kotlin 12-11-2017
Kotlin Variables Example Kotlin 11-11-2017
Kotlin Comments Example Kotlin 11-11-2017
Kotlin Hello World Kotlin 11-11-2017

1