Sling Academy
Home/Kotlin/Introduction to Loops in Kotlin

Introduction to Loops in Kotlin

Last updated: November 30, 2024

Loops are fundamental programming constructs that allow developers to repeat a block of code efficiently. Kotlin, being a statically-typed language on the JVM, provides several loop constructs to control the flow of execution. This article introduces loops in Kotlin and illustrates their practical usage through examples.

For Loop

The for loop in Kotlin is primarily used for iterating over a collection, or a range of numbers. Here's how you can implement a basic for loop in Kotlin:

// Iterating over a range of numbers
for (i in 1..5) {
    println("Iteration: $i")
}

This will output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5

The .. operator defines a range in Kotlin, making it easy to loop through numbers. If you prefer a different step, you can use step:

// Iterating over a range with a step
for (i in 1..10 step 2) {
    println("Step: $i")
}

While Loop

The while loop repeats the block of code as long as the given condition is true. It's useful when the number of iterations is not known beforehand.

var counter = 5

while (counter > 0) {
    println("Countdown: $counter")
    counter--
}

This code will display a countdown from 5 to 1.

Do-While Loop

The do-while loop functions similarly to the while loop, but it guarantees that the block of code will execute at least once. Here's an example:

var num = 0

do {
    println("Number: $num")
    num++
} while (num < 3)

Output:

Number: 0
Number: 1
Number: 2

Conclusion

Loops are an integral part of programming, allowing automation of repetitive tasks with ease. Whether iterating over data structures or performing a certain operation a specific number of times, Kotlin's loop constructs enable developers to write clear and efficient code.

Understanding these constructs will significantly enhance your ability to manipulate data and perform complex calculations efficiently in Kotlin.

Next Article: Using `for` Loops to Iterate Over Collections in Kotlin

Previous Article: When to Use `if-else` vs. `when` 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