Kotlin If Else If Statement

Chapter: Kotlin Last Updated: 18-11-2017 04:59:14 UTC

Program:

            /* ............... START ............... */
                
fun main(args: Array<String>) {

    var number = 2

    val result = if (number > 0)
        "positive number"
    else if (number < 0)
        "negative number"
    else
        "zero"

    println("Result is :  $result")

    number = 0

    val result1 = if (number > 0)
        "positive number"
    else if (number < 0)
        "negative number"
    else
        "zero"

    println("Result is :  $result1")
}
                /* ............... END ............... */
        

Output

Result is :  positive number
Result is :  zero

Notes:

  • If else if statment in kotlin makes a ladder of conditions, if one statement finds true it will exit from the if else if ladder.
  • In the above program you can see the if else if ladder with three conditions. If number > 0 it will take positive number as the return value, else if it will take the negative value.
  • If all condition is not satisfied it will take last else part.

Tags

If Else If Statement , Kotlin, if else if ladder

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 Nested If Kotlin 18-11-2017
Kotlin If Statement Kotlin 17-11-2017
Kotlin Print And Println Kotlin 17-11-2017
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