Signup/Sign In

Kotlin Continue, Break and Repeat Statement

In this tutorial we will discuss about continue, break and repeat statements in Kotlin. These statements are used within Kotlin loops to manage the execution flow of the loops for example, if you want to jump an iteration, or break out of the loop or repeat an iteration then these statements can be used.

We will cover all three of them with examples in this tutorial.

Kotlin continue Statement

The continue statement is used to skip the current iteration of the loop and start from next iteration. It is mostly used with if expression to decide whether to skip the current iteration of the loop based on a condition or not.

Let us understand continue statement with an example. We will print all the numbers from 1 to 10 except multiples of 3:

fun main() {
    for (number in 1..10){
        if (number % 3 == 0)
            continue
        println("Number is $number")
    }
}


Number is 1
Number is 2
Number is 4
Number is 5
Number is 7
Number is 8
Number is 10

Flow of this program:

  • For each value of number between 1 and 10, the loop will be executed.

  • Inside the if expression, it will check whether number is divisible by 3:

    • If yes, due to the continue statement, the rest of the loop will not be executed, hence the number won't be printed. It will go to the next value and execute the loop.

    • If no, then the loop will be executed and the println statement will be executed.

In case of nested loops the continue statement will be followed for the innermost loop.

Kotlin break Statement

The break statement is used to stop the execution of the loop and the execution exits or break out of the loop. It is also used with the if expression to break the loop based on certain condition.

Let us write a program to take input from user. It will continue to take input until user enters "stop":

fun main() {
    while (true){
        println("Enter the input: ")
        val input: String? = readLine()
        if(input == "stop")
            break
        println("Input entered is: $input")
    }
    println("Outside the loop!!")
}


Enter the input:
Hello
Input entered is: Hello
Enter the input:
World
Input entered is: World
Enter the input:
stop
Outside the loop!!

Flow of this program:

  • As the condition inside while loop is always true, it should never stop itself.

  • Input is taken and checked if it is "stop":

    • If yes, the loop will end and the last statement "Outside the loop" will be printed.

    • If no, the loop will continue to execute and print the input entered.

In case of nested loops, the break statement will break the innermost loop. Hence you will need multiple break statements to break out or nested loops, depending upon the number of loops.

Kotlin repeat Statement

The repeat statement is similar to loops in Kotlin. It is used to repeat a task a number of times, just like with loops.

The syntax of repeat statement is:

repeat(Number_of_time){
    // Statements to be executed
}

Let us use repeat statement to print a statement 3 times:

fun main() {
    repeat(3){
        i ->
        println("This statement will be printed 3 times!!")
        println("Index is: $i")
    }
}


This statement will be printed 3 times!!
Index is: 0
This statement will be printed 3 times!!
Index is: 1
This statement will be printed 3 times!!
Index is: 2

As shown in the code above, we can get the index of the repeat() statement if we want to use it. In the code above, we have used it to print the index.

Summary

In this tutorial we discussed about continue, break and repeat statements. In the next tutorial, we will start discussing about Kotlin String in detail.



About the author:
I'm a writer at studytonight.com.