"Mastering Kotlin: Loop Until with Ease - Tutorial & Examples"

Mastering Kotlin: Exploring the 'for' Loop with 'until'

In the realm of programming, Kotlin, a modern statically-typed programming language, offers a plethora of features that make it a popular choice for developers. One such feature is the 'for' loop, which allows for efficient iteration over a range of values. Today, we're going to delve into a specific aspect of this loop - the 'until' keyword.

Understanding the 'for' Loop in Kotlin

The 'for' loop in Kotlin is a powerful tool that enables you to iterate over a range of values, making it ideal for tasks like counting, summing, or performing any operation that requires sequential processing. The basic syntax of a 'for' loop is as follows:

for (i in start..end) {
    // code block
}

Here, 'start' and 'end' define the range of values, and the loop will iterate from 'start' to 'end', inclusive.

Android Kotlin while loop
Android Kotlin while loop

The 'until' Keyword: Exclusive Iteration

Now, let's introduce the 'until' keyword. Unlike the standard '..' operator, 'until' creates a range that excludes the end value. This can be particularly useful when you want to iterate over a sequence of numbers but don't want to include the final value in your operations. Here's the syntax:

for (i in start until end) {
    // code block
}

In this case, the loop will iterate from 'start' to 'end - 1'.

Comparing '..' and 'until'

To illustrate the difference between the two, let's consider an example. Suppose we want to print the numbers from 1 to 4. Using '..', we would write:

6. The Android Lifecycle | Android Programming with Kotlin for Beginners
6. The Android Lifecycle | Android Programming with Kotlin for Beginners

for (i in 1..4) {
    print(i)
}

This would output: 1234. Now, let's use 'until':

for (i in 1 until 4) {
    print(i)
}

This outputs: 123. As you can see, the 'until' keyword excludes the final value (4) from the output.

Use Cases of 'until'

The 'until' keyword can be particularly useful in scenarios where you want to perform an operation on a sequence of values but don't need to include the final value. For example, consider a function that calculates the sum of all numbers up to a given limit:

What is do while loop in Kotlin | How to use do while Loop in Kotlin in Bengali by Rajdip Mondal #12
What is do while loop in Kotlin | How to use do while Loop in Kotlin in Bengali by Rajdip Mondal #12

fun sumUpTo(n: Int): Int {
    var sum = 0
    for (i in 1 until n) {
        sum += i
    }
    return sum
}

In this case, using 'until' ensures that 'n' is not included in the sum.

Stepping Through the Range

You can also specify a step value when using 'until', allowing you to iterate over a range with a specific interval. Here's how you can do it:

for (i in start until end step stepValue) {
    // code block
}

For instance, to print the even numbers between 2 and 10, you could write:

for (i in 2 until 10 step 2) {
    print(i)
}

This would output: 246810.

Conclusion

The 'until' keyword in Kotlin's 'for' loop provides a convenient way to iterate over a range of values, excluding the final value. This can be particularly useful in a variety of scenarios, from simple printing tasks to more complex operations like calculating sums or performing other mathematical operations. By mastering the 'until' keyword, you'll be well-equipped to handle a wide range of programming tasks in Kotlin.

Free Kotlin Programming Book
Free Kotlin Programming Book
the kotlin roadmap logo is shown on a pink background with bubbles
the kotlin roadmap logo is shown on a pink background with bubbles
Learn Kotlin in a Week: The proven method to mastery
Learn Kotlin in a Week: The proven method to mastery
C++ Loops Explained in 1 Minute 🔁 | for, while, do-while Made Easy 💻✨
C++ Loops Explained in 1 Minute 🔁 | for, while, do-while Made Easy 💻✨
Kotlin — ‘for’ loop in 2 min (Updated - Jun, 2020)
Kotlin — ‘for’ loop in 2 min (Updated - Jun, 2020)
5 Best kotlin App Examplesf
5 Best kotlin App Examplesf
Top Kotlin Features must to Know
Top Kotlin Features must to Know
7 Kotlin Loop Statement | Online Training Download app from below link
7 Kotlin Loop Statement | Online Training Download app from below link
a white circle with the words python for loop and numbers = first, second, third
a white circle with the words python for loop and numbers = first, second, third
Loops in Kotlin
Loops in Kotlin
KOTLIN VS REACT NATIVE
KOTLIN VS REACT NATIVE
the loop engineering process is depicted in this graphic
the loop engineering process is depicted in this graphic
the text reads android app in kotlin is displayed above an image of a cell phone
the text reads android app in kotlin is displayed above an image of a cell phone
Ableton Tricks- Jam with Multiple Loops in one Clip
Ableton Tricks- Jam with Multiple Loops in one Clip
Kotlin-20-01 | Higher-Order function in Kotlin | Kotlin Tips | Kotlin with Rashid Saleem
Kotlin-20-01 | Higher-Order function in Kotlin | Kotlin Tips | Kotlin with Rashid Saleem
a computer screen with the words python for loop on it and an image of a programming program
a computer screen with the words python for loop on it and an image of a programming program
Kotlin — Try/Catch as Expression
Kotlin — Try/Catch as Expression
Kotlin Control Flow: if and when expressions, for and while loops
Kotlin Control Flow: if and when expressions, for and while loops
what to avoid infinitite loops
what to avoid infinitite loops
[Tự học Kotlin] Hàm mở rộng trong Kotlin
[Tự học Kotlin] Hàm mở rộng trong Kotlin
achtergrond
achtergrond
an image of a computer screen with the text 5 nested loops loops inside other loops, often used for multi - dimensional data
an image of a computer screen with the text 5 nested loops loops inside other loops, often used for multi - dimensional data
the text 4 for each loop used to treat over arrays and collections without dealing with indexes
the text 4 for each loop used to treat over arrays and collections without dealing with indexes