1. What are Kotlin Conditions?

In this tutorial, we will learn about the mighty if-else conditions or expressions, which enhance the power of Kotlin language. In the ever-evolving world of programming languages, Kotlin outweighs its peers with its robust approach to dealing with conditional logic. Usually, the code flow is controlled by using the fundamental building block of Kotlin expression i.e. if-else statements.

In this detailed tutorial, we will explore Kotlin's conditional expressions, understand their syntax and different variants in use, the nested if-else structure, some advanced techniques, and delve into the best practices to write efficient code. Kotlin if-else Expression Flowchart

Tutorial Contents:

  1. Kotlin Conditions
  2. Kotlin if Expression
  3. The else Expression
  4. Kotlin else if Expression
  5. Nested if-else Conditions
  6. Advanced Techniques
  7. Best Practices For Kotlin Conditions

2. Kotlin if Expression

The Kotlin programming is equipped with the if expression like any other coding language. The basic structure or syntax of the Kotlin if expression is below.

val x = 20
val y = 10
if (x > y) {
	println("Good Morning")
}

2.1. if Expression Basics

  • We use the if keyword to define this conditional expression.
  • Importantly, we can only use lowercase if.
  • Using If or IF keywords will throw an error.
  • The code logic comes immediately after the if keyword, within the round brackets.
  • The code to run if the logic fulfills is enclosed within the curly brackets, right after the code logic.

3. Kotlin else Expression

The else expression comes after the if expression. Basically, it is executed when the if condition is false.

val x = 20
val y = 10
if (x > y) {
	println("Good Morning")
} else {
	println("Good Evening")
}

3.1. else Expression Basics

  • The else expression never comes alone.
  • Whenever the if expression fails, the else block triggers.
  • We will always write lowercase else letters, similar to if expression.
  • If the logic of the if expression returns true, the else block will not be executed.

4. Kotlin else-if Expression

Unlike the if-else expression, where we only check one condition, Kotlin also allows us to check multiple conditions using the else-if expression. This is useful when we need to check multiple conditions, and when a certain condition is fulfilled, the code block after that particular condition is executed.

val time = 25
if (time < 10) {
	println("Good Morning")
} else if (time < 20) {
	println("Good Nood")
} else {
	println("Good Evening")
}

4.1. else-if Expression Basics

  • The else-if expression always comes with more than one condition.
  • Whenever the if expression fails, the else-if is checked.
  • We use lowercase letters for else-if expressions.

5. Kotlin Nested if-else Conditions

In Kotlin language, we can also create the base of an if-else expression on another conditional expression. We can do so by nested conditions where one decision is based upon an additional logic. In this way, nested if-else expressions enable the developers to check their logic deeply.

For example, imagine checking a user's age and then, based on their age group, offering them age-appropriate options:

Example

if (age >= 18) {
	if (age >= 65) {
		print("Spend time with buddies.")
	} else {
		print("Spend time with parteners.")
	}
} else {
	print("Spend time with sibilings.")
}

6. Advanced Techniques To Use Kotlin Conditions

There are certain advanced methods and techniques to work with the if-else expressions in Kotlin language. These advanced techniques are listed below and explained with the help of examples.

  1. Using if-else as an expression
  2. Using if-else as a single expression without braces

6.1. Kotlin if-else As Expression

Importantly, we can also assign an if-else statement to a variable as an expression. Use of else is a must while using an if-else statement as an expression.

Example

val time = 25
val greetingMsg = if (time < 10) {
	"Good Morning"
} else {
	"Good Evening"
}
println(greetingMsg)
Good Evening

6.2. Kotlin if-else As Single Expression

This special Kotlin technique allows us to write if-else expressions without the braces, and we can only do so if there is only a single expression. We can rewrite the above instance in a single expression as below. Also, there is no ternary operator in Kotlin language as ordinary if works fine.

Example

val time = 25
val greetingMsg = if (time < 10) "Good Morning" else "Good Evening"
/* Display the Greeting Message */
println(greetingMsg)
Good Evening

7. Best Practices For Kotlin Conditions

Like any powerful tool, using the if-else conditions in Kotlin demands some mindful practice. And for that purpose, we are sharing some best practices and guiding principles that are to be kept in mind, while writing the conditional expressions in Kotlin code.

  • Concise : Keep your Kotlin conditions clear and concise avoiding redundant and overly complex checks.
  • Readability Matters : Always write meaningful code and conditions that are easy to read and understand.
  • Nesting : Be cautious while writing nested if-else statements and prefer when expression for increased clarity.
  • Documentation : Must document your code logic to explain the decision-making process.
  • Nullability : You should account for nullability in your conditions by incorporating the Kotlin safe call operator(?.) and Elvis operator (?:) to provide default values.
  • Testing : Always test your code conditions thoroughly for all possibilities to avoid embarrassing situations.
Give Us Your Feedback
OR
If You Need Any Help!
Contact Us