Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
In Kotlin, control flow structures like if-else
are expressions, meaning they can return a value. This is a significant departure from many traditional programming languages and allows for more concise and expressive code.
Here's a tutorial on the if-else
expression in Kotlin:
Just like in many languages, the if-else
structure can be used without returning a value.
val number = 10 if (number > 5) { println("Number is greater than 5") } else { println("Number is less than or equal to 5") }
The ability to return a value from an if-else
expression allows you to assign the result directly to a variable.
val number = 10 val result = if (number > 5) "Greater" else "Lesser" println(result) // Prints: Greater
You can add as many else if
clauses as necessary:
val score = 85 val grade = if (score >= 90) { "A" } else if (score >= 80) { "B" } else if (score >= 70) { "C" } else { "D" } println(grade) // Prints: B
If you have multiple statements in the block of your if
or else
, the last expression in the block is what gets returned.
val number = 7 val result = if (number % 2 == 0) { println("Even number") "Even" } else { println("Odd number") "Odd" } println(result) // Prints: Odd number followed by Odd
When you omit the else
clause in an if
expression, the omitted branch will return the special value Unit
(similar to void
in other languages).
val number = 5 val isEven = if (number % 2 == 0) true else Unit println(isEven) // Prints: kotlin.Unit
if-else
expressions can be nested for more complex conditions.
val number = 15 val result = if (number > 0) { if (number % 2 == 0) { "Positive Even" } else { "Positive Odd" } } else if (number == 0) { "Zero" } else { "Negative" } println(result) // Prints: Positive Odd
The if-else
expression in Kotlin provides a powerful and concise way to handle conditional logic. By being able to return values, it simplifies many scenarios where you would traditionally use a ternary conditional operator (like ? :
in Java or C++). Familiarizing yourself with this concept can help streamline your Kotlin code.
Conditional statements in Kotlin with if-else:
val x = 10 if (x > 5) { println("x is greater than 5") } else { println("x is not greater than 5") }
Single-line vs multi-line if-else in Kotlin:
val result = if (x > 5) "Greater" else "Not Greater"
Using if-else expression with assignments in Kotlin:
val status = if (x > 0) "Positive" else "Non-positive"
Chaining if-else expressions in Kotlin:
val grade = if (score > 90) "A" else if (score > 80) "B" else "C"
Nested if-else expressions in Kotlin:
val category = if (x > 0) { if (x % 2 == 0) "Positive Even" else "Positive Odd" } else { "Non-positive" }
Kotlin if-else expression vs when expression:
val result = when { x > 5 -> "Greater" x == 5 -> "Equal" else -> "Less" }
Ternary operator equivalent in Kotlin:
val max = if (a > b) a else b
If-else expression with ranges in Kotlin:
val rangeResult = if (x in 1..10) "In range" else "Out of range"
Equality and comparison in if-else expression in Kotlin:
==
and !=
for equality and inequality.val message = if (x == 0) "Zero" else "Non-zero"
Handling nullability with if-else expression in Kotlin:
val length = if (text != null) text.length else 0
Using if-else expression in Kotlin functions:
fun isPositive(num: Int): Boolean { return if (num > 0) true else false }
Common pitfalls with if-else expressions in Kotlin:
val value = if (condition) "True" // Incorrect: Missing else part