Kotlin If-Else on one line short (with Examples)

Kotlin If-Else on one line short (with Examples) 🩳

Tags:
Kotlin

In Kotlin, you can have if-else statements on one line. To write an if-else statement on one line, follow the conditional expression syntax:

if (condition) some_expression else other_expression
if (condition) some_expression else other_expression

For example:

fun main() { val age = 16 // One-liner if-else statement val age_group = if (age < 18) "Minor" else "Adult" println(age_group) }
fun main() { val age = 16 // One-liner if-else statement val age_group = if (age < 18) "Minor" else "Adult" println(age_group) }

Output in the console:

console
Minor
console
Minor

This is useful with shorter if-else statements because they allow you to write fewer lines of code while maintaining readability. However, avoid turning longer if-else statements into one-liners. Doing so can cause your code to lose clarity.

Four ways of If-Else Statements in Kotlin

To summarize the different methods of the if-else statement, here are three different variants of the same if-else query.

Methode 1 - The normal if else

fun main() { val age = 16 var age_group: String if (age < 18) { age_group = "Minor" } else { age_group = "Adult" } println(age_group) // Output: Minor }
fun main() { val age = 16 var age_group: String if (age < 18) { age_group = "Minor" } else { age_group = "Adult" } println(age_group) // Output: Minor }

Methode 2 - Without curly brackets

fun main() { val age = 16 var age_group: String if (age < 18) age_group = "Minor" else age_group = "Adult" println(age_group) // Output: Minor }
fun main() { val age = 16 var age_group: String if (age < 18) age_group = "Minor" else age_group = "Adult" println(age_group) // Output: Minor }

Methode 3 - One-liner

fun main() { val age = 16 // One-liner if-else statement val age_group = if (age < 18) "Minor" else "Adult" println(age_group) // Output: Minor }
fun main() { val age = 16 // One-liner if-else statement val age_group = if (age < 18) "Minor" else "Adult" println(age_group) // Output: Minor }

Methode 4 - When statement

fun main() { val age = 16 var age_group: String when(age < 18) { true -> age_group = "Minor" false -> age_group = "Adult" } println(age_group) // Output: Minor }
fun main() { val age = 16 var age_group: String when(age < 18) { true -> age_group = "Minor" false -> age_group = "Adult" } println(age_group) // Output: Minor }

All these methods achieve the same goal in different ways. Which of these methods you should use depends very much on the application and can therefore not be said so sweepingly.

Special case null check

If you have a nullable reference and you want to use that reference you have to check if the reference is not null. To do that you could so it in a one-line if else check, like so:

val l: Int = if (b != null) b.length else -1 // Output: b = null => -1 // Output: b != null => b.length
val l: Int = if (b != null) b.length else -1 // Output: b = null => -1 // Output: b != null => b.length

Instead of writing the complete if expression, you can also express this with the Elvis operator ?:

val l = b?.length ?: -1
val l = b?.length ?: -1

If the expression to the left of ?: is not null, the Elvis operator returns it, otherwise it returns the expression to the right. Note that the expression on the right-hand side is evaluated only if the left-hand side is null.

Thanks for reading. Happy coding!