Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.7 Loop Control Statements With Labelled `for` Loops

In the previous lessons we have learned about the loops and the iterators. In this lesson, we will be learning about the loop control statements. The loop control statements in Kotlin are basically break and continue. We also have written as the loop control statement. But here in this lesson we will be learning about break and continue. So to save some time, I have written a block of code here. I have written a for loop for which the counter variable is i, having the range from 1 to 5. And inside the for loop, there is another for loop where the counter variable is g, having the range from 1 to 5. And inside that, I have written a print statement which says i is equals to $i. And then it says j is equals to $j. Let us run the code and see what happens here before we start learning break and continue with the deliberate for loop. Here we have i is equal to 1, j is equal to 1. It was printed from i is 1 to j is 5. And then we have so many values here which continues til i is 5 and j is 5. So we are good up til this. Say for some reason, I want to come out of the loop when i is 3 and j is 3. For some reason I do not want to continue any more, so what do I do? Let me write the condition here. Now if this condition is satisfied, come out of the loop. To come out of the loop, we will use the break keyword, so break. If i is equal to equal to 3 and j is equal to equal to 3, come out of the loop and I don't want to print anything else. Let's run this block of code, and see what happens. So, here we are. I have i == 3 and j == 3. Yes, the execution has stopped and the break has been implemented. But the execution is broken from this for loop in which the break statement is written. But I want to come out of this for loop. And I don't want any execution further. So what do I do for it? Kotlin has provided us with labelling of the for loop. We will label our for loops here. Let's say primary loop is for my first loop. And then for the second loop, I will have name say secondary loop. So the syntax for labeling the loop is writing the name of the loop. Of course, you can change this, followed by add rate. And I'll use this names with the break statement to come out of the loop. Break at the rate primary loop. So when we use this labels with some statement, we write it at the rate and the loop name. Now, let us run our code and see what happens. Here we are. We have come out of the primary loop, and there is no execution of the loop anymore. We have successfully used the label for loop with break. In case if we haven't mentioned the loop from which to break out of, the immediate for loop in which the break statement is written will be considered. But when we mention the name of the loop here, it comes out and breaks out of the primary loop. And there is no more execution of the code. And so we are clear with using break with labeled for loop. Let us jump and see how to use continue with labeled for loop. Let me change this range to 3 for our better understanding. So here we are. We have i is 1, j is 1, i is 1, j is 2. Like this up to i is 3 and j is 3. Now for some reason, I don't want the loop to continue when the value of i is 2 and j is 2. So let's write the statement here. Then continue. So here let's see what's the output? Here we have i = 2 and j = 1. The case where i is 2, and j is 2 is skipped. But again we have i is 2 and j is 3, which is undesirable. I wanted my loop to continue from the primary loop, but it has continued from the secondary loop. Because the immediate for loop for continue was this loop, which has j as the countervariable. To come out and continue with the main loop or the primary loop, I need to mention here primary loop. Now on running this. This is what I wanted. I have i = 2 and j = 1. But when i is 2 and j is 2, it completely comes out of the loop, and the value of i is increased, and then we have 3. You see, we have i is 3, and j is 1, and then the loop continues normally. This is how you need to use the label for loop with continue. This is all for this lesson on loop control statements with break and continue. These are the basics of Kotlin that we have been learning in the previous lessons. In the next module, I'll show you a glimpse of how great Kotlin is. How interesting it is. We will have the introduction to the high level functions and the Lambdas. How do we use Java and Kotlin interoperably? How you can call the class of Kotlin from a Java file and how you can call the Java class from a Kotlin file. So stay tuned for the next lessons. Keep smiling and have a good day.

Back to the top