Sling Academy
Home/Kotlin/Introduction to Control Flow in Kotlin

Introduction to Control Flow in Kotlin

Last updated: November 30, 2024

Kotlin is an expressive programming language that simplifies common coding tasks. One fundamental concept you need to master in Kotlin is control flow. Control flow statements allow you to govern the execution path of your code.

If-Else Statements

The if-else statement is a fundamental control flow statement that executes some code if a particular condition is true, and possibly different code if it is not.

val number = 5

if (number > 0) {
    println("Number is positive")
} else {
    println("Number is negative or zero")
}

You can also use if as an expression:

val result = if (number % 2 == 0) "Even" else "Odd"
println(result)

When Statements

The 'when' statement in Kotlin is similar to the switch-case statement in other languages. It is more powerful and can be used as both a statement and an expression.

val day = "Monday"

when (day) {
    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday")
    "Saturday", "Sunday" -> println("Weekend")
    else -> println("Not a valid day")
}

When is also used as an expression:

val response = when (number) {
    in 0..10 -> "Single-digit"
    !in 0..10 -> "Multiple-digits"
    else -> "None"
}
println(response)

For Loops

Kotlin offers a variety of looping constructions. The for loop is commonly used to iterate over ranges, arrays, or other iterable objects.

for (i in 1..5) {
    println(i)
}

You can also traverse arrays:

val array = arrayOf(1, 2, 3, 4, 5)
for (i in array) {
    println(i)
}

While Loops

The while loop repeats a block of code as long as a specified condition is true.

var x = 0

while (x < 5) {
    println(x)
    x++
}

A do-while loop is similar to a while loop but checks the condition after executing the code block, ensuring the code block is run at least once.

var y = 0

do {
    println(y)
    y++
} while (y < 5)

These control flow constructs provide foundational skills for programming in Kotlin, enabling you to create more sophisticated logic and handle complex problems effectively.

Next Article: Understanding Conditional Statements 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