Sling Academy
Home/Kotlin/Understanding Conditional Statements in Kotlin

Understanding Conditional Statements in Kotlin

Last updated: November 30, 2024

Introduction

Conditional statements are fundamental building blocks in Kotlin programming. They allow developers to perform different actions based on different conditions. In this article, we'll explore how to effectively use conditional statements in Kotlin, with various examples.

If Statement

The if statement in Kotlin is used to test a condition. If the condition evaluates to true, the code block inside the if is executed.

fun main() {
    val number = 10
    if (number > 5) {
        println("The number is greater than 5")
    }
}

If-Else Statement

The if-else statement provides an alternative path of execution if the condition evaluates to false.

fun main() {
    val number = 10
    if (number > 15) {
        println("The number is greater than 15")
    } else {
        println("The number is 15 or less")
    }
}

If-Else If-Else Ladder

If you have multiple conditions to evaluate, you can use the if-else if-else ladder.

fun main() {
    val number = 20
    if (number < 10) {
        println("The number is less than 10")
    } else if (number < 20) {
        println("The number is between 10 and 19")
    } else {
        println("The number is 20 or greater")
    }
}

When Statement

The when statement in Kotlin offers a more readable and concise way of handling multiple conditions. It can be used as an alternative to the switch-case concept in other programming languages.

fun main() {
    val dayOfWeek = 4
    val dayName = when(dayOfWeek) {
        1 -> "Monday"
        2 -> "Tuesday"
        3 -> "Wednesday"
        4 -> "Thursday"
        5 -> "Friday"
        6 -> "Saturday"
        7 -> "Sunday"
        else -> "Invalid day"
    }
    println("Today is $dayName")
}

Using Conditional Expressions

Kotlin allows if and when statements to behave as expressions. This means you can directly use them to assign values depending on conditions.

fun main() {
    val age = 18
    val status = if (age >= 18) "adult" else "minor"
    println("Status: $status")

    val result = when(age) {
        in 0..12 -> "child"
        in 13..19 -> "teen"
        else -> "adult"
    }
    println("Age group: $result")
}

Conclusion

Understanding and utilizing conditional statements effectively is crucial for controlling the flow of applications in Kotlin. By mastering these constructs, you will be able to write more dynamic and responsive code.

Next Article: How to Use `if-else` in Kotlin

Previous Article: Introduction to Control Flow in Kotlin

Series: Control Flow in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin