Break Or Labeled Break In Kotlin With Examples




In this article, we will learn to use break in kotlin. We will see how to use break with while loop, do while loop or for loop in kotlin. After that, we will also go through labeled break and unlabeled break in kotlin. At last, we will see some questions and examples for break in kotlin.

When we work with loop in program, sometimes, we need to come out from the loop if certain conditions are satisfied. In such condition, we either use return or break within loop in the program. In this article, we will learn to use break in such condition. break terminates the nearest enclosing loop in the program.

There are 2 ways to use break in Kotlin programming.
a. Without Label(UnLabeled Break).
b. With Label(Labeled Break)

Now, we will go through each way one by one.

How to use Unlabeled Break in Kotlin(Or Simply Break in Kotlin)

Unlabeled Break in Kotlin terminates nearest enclosing loop in program when encountered, that too, without checking the test expression.

You can use break in for loop , while loop or do while loop in kotlin. We will go through it one by one.

Flow Diagram to use Break in while loop in Kotlin

Tutorialwing - Flow diagram of break in while loop in kotlin

Tutorialwing – Flow diagram of break in while loop in kotlin

Looking into the flow diagram, it is clear that loop is terminated as soon as break is encountered.

Syntax to use Break in while loop in Kotlin

Now, Let’s see the syntax to use break in while loop.

Tutorialwing - Syntax to use Break in while loop in Kotlin

Tutorialwing – Syntax to use Break in while loop in Kotlin

Examples to Show Break in while loop in kotlin

Now, we will see different examples that demonstrates the use of break in while loop.

1. An example to demonstrates the use of break in kotlin.

fun main(args: Array<String>) {

	var y = 10
	while (y >= 0) {
	    if (y % 2 == 0) {
	        break;
    	    }
	    println(y);
	    y--;
	}
}

When you run the program, you will get below output:
You will get nothing as output.
Let’s see the reason.

Round 1
y == 0 , 10 % 2 == 0.
Condition is true. So, code inside if block will be executed. Since there is a break statement in if block, it will terminate nearest enclosing loop in the program. Hence, while loop will be terminated. Finally, Nothing will be printed.

2. Guess the output of below program
fun main(args: Array<String>) {

	var y = 10
	while (y >= 0) {
	    println(y * y * y)
	    y--;
	    break;
	    println(y)
	}
}
3. Guess the output of below program
fun main(args: Array<String>) {

	var index = 5
	while(index != 0) {
	    if(index == 0) break;
	    println(index);
   	    index -= 2;
	}
}



Flow Diagram to use Break in do while loop in Kotlin

We have seen how to use break in while loop in kotlin, now, we will see how to use kotlin break in do while loop.

Tutorialwing - Flow diagram to use kotlin break in do while loop in program

Tutorialwing – Flow diagram to use kotlin break in do while loop in program

Syntax to use Break in do while loop in Kotlin

Now, let’s see the syntax to use break in do while loop in kotlin.

Tutorialwing - Syntax to use break in do while loop in kotlin

Tutorialwing – Syntax to use break in do while loop in kotlin

Examples to Show Break in do while loop in kotlin

In this section, you will learn how to use break in do while loop in kotlin. We will go through different examples to demonstrates the use of break in kotlin.

1. Use of break in do while loop in kotlin

fun main(args: Array<String>) {

	val x = 1
	do {
	    println("Hi Tutorialwing");
	    if (true)
	        break

	} while (x > 0);
}

When you run the program, you will get below output:
Hi Tutorialwing

Notice the condition inside if block. it’s always true. So, break will terminate the do while loop at first iteration itself.

2. Guess the output of below program
fun main(args: Array<String>) {

	val x = true
	do {
	    println("Hi Tutorialwing");
	    break;
	} while (x);
}
3. Guess the output of below program
fun main(args: Array<String>) {

	val x = false
	do {
	    println("Hi Tutorialwing");
	    if (x)
        		break
	} while (x);

}



Flow Diagram to use Break in for loop in Kotlin

Till now, we have seen how to use break in while loop and do while loop in kotlin. Now, we will see how to use kotlin break in for loop. At first, let’s see the flow diagram of break in for loop.

Tutorialwing - Flow diagram to use break in for loop in kotlin

Tutorialwing – Flow diagram to use break in for loop in kotlin

Syntax to use Break in for loop in Kotlin

Now, Let’s see syntax to use break in for loop in kotlin.

Tutorialwing - break in for loop in kotlin

Tutorialwing – break in for loop in kotlin

Examples to Show Break in for loop in kotlin

In this section, you will learn how to use break statement in for loop in kotlin.

1. An example to demonstrate the use of break in for loop

fun main(args: Array<String>) {

	for (i in 1..10) {
	    if (i == 5)
	        break

	    println(i)
	}
}

When you run the program, you will get below output:
1
2
3
4

Note that we are checking if i is equal to 5 or not at each iteration. So, loop is terminated when i == 5.

2. Guess the output of below program
fun main(args: Array<String>) {

	for (i in 6 downTo 1 step 2) {
	    if(i == 2)
	        break

	    println(i)
	}
}
3. Guess the output of below program
fun main(args: Array<String>) {

	for (i in 5 downTo 1 step 2) {
	    if(i == 0)
	        break

	    println(i)
	}
}
4. Guess the output of below program
fun main(args: Array<String>) {

	val books = arrayOf("Harry Potter", "Black Beauty", "The Alchemist", "The Ginger Man", "The Hite Report")
	for (index in books.indices) {
	    if(index == 1)
	        break

	    println(books[index])
	}
}

How to use Labeled Break in Kotlin ?

As we know break in kotlin is used to terminate the enclosing loop in the program. What if we want to terminate the outer loop when break statement is encountered? Or, how can we terminate desired loop when break statement is encountered?
We can achieve the target using labeled break in kotlin. This is brilliant feature in kotlin. You can terminate the desired loop using label in break. You just need to provide the break@labelname to break the loop having label labelname. That’s it. Here, we are just providing an example to demonstrate the use of labeled break in kotlin.

WAIT A MINUTE!

Interested to know more about Labeled break in kotlin, you can visit Kotlin Labeled Break

1. Examples to demonstrates the use of labeled break in for loop

fun main(args: Array<String>) {
    first@ for (i in 1..10) {

        second@ for (j in 1..5) {
            println("i = $i; j = $j")

            if (i == 2)
                break@first
        }
    }
}

When you run the program, you will get below output:
i = 1; j = 1
i = 1; j = 2
i = 1; j = 3
i = 1; j = 4
i = 1; j = 5
i = 2; j = 1

We are checking whether i is equal to 2 or not in each iteration of inner for loop. If yes, code inside if expression block will be executed. We have break@first in if expression block. It means terminate the first loop. Notice that first is outer loop.




Conclusion

In this post, we have seen how to use break in Kotlin. You will learn how to use break in for loop, while loop or do while loop in kotlin. We have also gone through labeled break in kotlin. You can also checkout our tutorial on Kotlin Labeled Break

Support Us

If you have any suggestion about this post, please comment below. We are looking for someone who helps use to make our tutorial better. Also, If you want tutorials on any topic, you can mail us at tutorialwing@gmail.com. We will try to cover the topic as soon as possible. Connect with us on Facebook, Google+, Twitter and Youtube for more updates.