Sling Academy
Home/Kotlin/How to Use `for` Loops with Ranges in Kotlin

How to Use `for` Loops with Ranges in Kotlin

Last updated: November 30, 2024

In Kotlin, for loops are a powerful way to iterate over a range of numbers. This type of loop is essential for scenarios where you need to execute a block of code a specific number of times. In this article, we'll explore how to use for loops with ranges in Kotlin, and look at some practical examples.

Using Ranges in Kotlin

A range in Kotlin is defined using the .. operator. For instance, a range from 1 to 5 is written as 1..5. Kotlin also offers a rangeTo() function that provides the same functionality. Here's how you define a range:

val range = 1..5

Basic for Loop with Ranges

The basic syntax for a for loop using ranges in Kotlin is straightforward:

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

This loop will print numbers from 1 to 5 to the console. The variable i iterates over each number in the range.

Using downTo for Reverse Order

If you need to iterate over a range in reverse order, you can use the downTo keyword:

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

This will print numbers in descending order from 5 to 1.

Specifying Steps in a for Loop

You may also specify step values to skip numbers within the range. This is done using the step keyword:

for (i in 1..10 step 2) {
    println(i)
}

In this loop, starting from 1, every second number will be printed, resulting in the output of 1, 3, 5, 7, 9.

Example: Summing Numbers in a Range

Here's a full example where we use a for loop with a range to calculate the sum of numbers from 1 to 100:

var sum = 0
for (i in 1..100) {
    sum += i
}
println("Sum: $sum")

This will calculate and print the sum of numbers from 1 to 100, which is 5050.

Conclusion

Using for loops with ranges in Kotlin provides a convenient way to work with sequences of numbers. Whether iterating in ascending, descending order, or with a specific step, Kotlin's range capabilities make loop handling intuitive and efficient. By understanding these basics, you can effectively harness the power of iteration in your Kotlin programs.

Next Article: Iterating Through Maps with `for` Loops in Kotlin

Previous Article: Using `for` Loops to Iterate Over Collections 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