Kotlin If Else If Statement
Chapter:
Kotlin
Last Updated:
22-09-2018 06:15:28 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.