Kotlin Loops: Mastering the Power of Continue Learn how to use the `continue` construct in Kotlin to skip iterations and write more efficient loops. Discover the basics of `continue`, its use with `if…else`, and how to calculate sums of positive numbers. Plus, explore labeled `continue` and best practices for code readability.

Mastering Loops in Kotlin: The Power of Continue

When working with loops, there are times when you need to skip the current iteration and move on to the next one. This is where the continue construct comes in, allowing you to bypass the rest of the code inside the loop for that particular iteration.

The Basics of Continue

So, how does continue work its magic? It’s often used in conjunction with the if...else construct. For instance, if a certain condition is met, continue is executed, skipping all the code inside the loop after it for that iteration. Let’s take a look at an example:

kotlin
var i = 0
while (i < 10) {
i++
if (i > 1 && i < 5) {
continue
}
println("The value of i is $i")
}

When you run this program, the output will be:

The value of i is 5
The value of i is 6
The value of i is 7
The value of i is 8
The value of i is 9

As you can see, when i is greater than 1 and less than 5, continue is executed, skipping the execution of the println statement. However, the i++ statement is still executed in each iteration of the loop because it comes before the continue construct.

Calculating the Sum of Positive Numbers Only

Let’s take a look at a more practical example. Suppose you want to calculate the sum of the maximum of 6 positive numbers entered by the user. If the user enters a negative number or zero, you want to skip it from the calculation. Here’s how you can do it:

kotlin
var sum = 0
var count = 0
while (count < 6) {
print("Enter a number: ")
val num = readLine()!!.toInt()
if (num <= 0) {
continue
}
sum += num
count++
}
println("The sum of the positive numbers is $sum")

Labeled Continue: Skipping the Iteration of a Specific Loop

What you’ve learned so far is the unlabeled form of continue, which skips the current iteration of the nearest enclosing loop. But did you know that continue can also be used to skip the iteration of a specific loop (even an outer loop) by using labels? A label in Kotlin starts with an identifier followed by @. Here’s an example:

kotlin
outerloop@ while (true) {
innerloop@ while (true) {
print("Enter a number: ")
val num = readLine()!!.toInt()
if (num == 0) {
continue@outerloop
}
if (num < 0) {
continue@innerloop
}
println("The number is $num")
}
}

When you run this program, the output will be:

Enter a number: 5
The number is 5
Enter a number: 0
Enter a number: -3
Enter a number: 7
The number is 7

As you can see, using labeled continue allows you to skip the iteration of a specific loop. However, it’s worth noting that the use of labeled continue is often discouraged because it can make your code harder to understand. If you find yourself in a situation where you need to use labeled continue, try to refactor your code to make it more readable.

Wrap-Up

In Kotlin, there are three structural jump expressions: break, continue, and return. By mastering continue, you can write more efficient and effective loops. Remember to use labeled continue sparingly and always prioritize code readability.

Leave a Reply

Your email address will not be published. Required fields are marked *