if Expressions
| Since: | Kotlin 1.0(2016) |
|---|
In Kotlin, if can be used not only as a statement but also as an expression. You can assign the result of a conditional branch to a variable or return it directly from a function.
Syntax
if (condition) {
// process
} else if (condition) {
// process
} else {
// process
}
// Used as an expression (returns a value)
val variable = if (condition) valueA else valueB
Syntax List
| Syntax | Description |
|---|---|
| if (condition) | Executes the block when the condition is true. |
| else if (condition) | Evaluates an additional condition when the previous condition is false. |
| else | The block that runs when all conditions are false. |
| if expression | The last expression in the block is returned as the value. Use this instead of the ternary operator. |
Sample Code
sample_if_expression.kt
fun main() {
val score = 75
// Basic if / else if / else
if (score >= 90) {
println("Excellent.")
} else if (score >= 60) {
println("Pass.") // Prints "Pass."
} else {
println("Fail.")
}
// Assign to a variable using an if expression (instead of a ternary operator)
val result = if (score >= 60) "Pass" else "Fail"
println(result) // Prints "Pass"
// When using blocks, the last expression becomes the value
val grade = if (score >= 90) {
println("Excellent!")
"A"
} else if (score >= 70) {
println("Good!") // Prints "Good!"
"B"
} else {
"C"
}
println(grade) // Prints "B"
// You can also use an if expression in a function return
val x = 10
val abs = if (x >= 0) x else -x
println("Absolute value: $abs") // Prints "Absolute value: 10"
// Also useful for null checks
val name: String? = "Kotlin"
val greeting = if (name != null) "Hello, $name" else "No name provided"
println(greeting) // Prints "Hello, Kotlin"
}
if_expression.kt
kotlinc if_expression.kt -include-runtime -d if_expression.jar java -jar if_expression.jar Pass. Pass Good! B Absolute value: 10 Hello, Kotlin
Common Mistakes
sample_if_mistakes.kt
fun main() {
val score = 75
// Using if as an expression without an else branch
// val grade = if (score >= 60) "Pass" // Compile error (else is required)
// Misunderstanding which expression is returned in a block
val label = if (score >= 90) {
println("Excellent") // println is not the return value
"A" // This is the return value
} else {
"B"
}
println(label) // B
// Writing if as a statement and trying to assign it to a variable
// var result: String
// if (score >= 60) result = "Pass" else result = "Fail" // Works but verbose
// val result2 = if (score >= 60) "Pass" else "Fail" // Idiomatic Kotlin
// Using a Unit-returning block as an expression unintentionally
val x = if (score > 0) println("Positive") else println("Non-positive")
println(x) // kotlin.Unit
}
The command looks like this:
kotlinc sample_if_mistakes.kt -include-runtime -d sample_if_mistakes.jar java -jar sample_if_mistakes.jar B Positive kotlin.Unit
Notes
Kotlin does not have Java's ternary operator (? :). Instead, you can use if as an expression to achieve the same result. When using if as an expression, an else branch is always required. Without else, the value cannot be determined and a compile error will occur.
For branching on multiple conditions, the when expression is a better fit. The ?: Elvis operator is also handy when working with nullable types.
If you find any errors or copyright issues, please contact us.