Kotlin Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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") } |
Output:
1 2 3 4 | Result is : positive number Result is : zero |
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.