Kotlin if Expression: In this tutorial, you will learn to use the if expression in Kotlin with the assistance of examples.
In Kotlin, if is an expression is which returns a value. It is used to control the flow of the program structure.
In this article, you will learn-
Traditional Usage of if…else
The syntax of if…else is:
if (testExpression) { // codes to run if testExpression is true } else { // codes to run if testExpression is false }
in the event that executes a specific segment of code if the testExpression is assessed to true. It can have optional else statement. Codes inside else condition are executed if the testExpression is false.
Example: Traditional Usage of if…else
fun main(args: Array<String>) { val number = -10 if (number > 0) { print("Positive number") } else { print("Negative number") } }
At the point when you run the program, the output will be:
Negative number
Kotlin if expression
In contrast to Java (and other many programming languages), if can be used as an expression in Kotlin; it returns a worth. Suggested Reading: Kotlin expression
Here is an example:
Example: Kotin if the expression
fun main(args: Array<String>) { val number = -10 val result = if (number > 0) { "Positive number" } else { "Negative number" } println(result) }
At the point when you run the program, the output will be:
Negative number
The else branch is required when using if as an expression.
The curly braces are discretionary if the body of if has just a single proclamation. For instance,
fun main(args: Array<String>) { val number = -10 val result = if (number > 0) "Positive number" else "Negative number" println(result) }
Example: if block With Multiple Expressions
On the off chance that the block of if branch contains more than one expression, the last expression is returned as the value of the block.
fun main(args: Array<String>) { val a = -9 val b = -11 val max = if (a > b) { println("$a is larger than $b.") println("max variable holds value of a.") a } else { println("$b is larger than $a.") println("max variable holds value of b.") b } println("max = $max") }
At the point when you run the program, the output will be:
-9 is larger than -11. max variable holds value of a. max = -9
Kotlin if..else..if Ladder
You can return a block of code among numerous blocks in Kotlin using if..else…if ladder.
Example: if…else…if Ladder
fun main(args: Array<String>) { val number = 0 val result = if (number > 0) "positive number" else if (number < 0) "negative number" else "zero" println("number is $result") }
This program checks whether number is positive number, negative number, or zero.
Kotlin Nested if Expression
An if expression can be inside the block of another if expression known as nested if expression.
Example: Nested if Expression
This program computes the largest number among three numbers.
fun main(args: Array<String>) { val n1 = 3 val n2 = 5 val n3 = -2 val max = if (n1 > n2) { if (n1 > n3) n1 else n3 } else { if (n2 > n3) n2 else n3 } println("max = $max") }
At the point when you run the program, the output will be:
max = 5
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.