Welcome.
Now it's time to learn about the label statements, they allow you to jump to a label using a break,
continue, or I go to statement.
Let's start by exploring the labeled brake statement now.
In this first example, you learn how to quit label loop.
OK, let's suicide.
Remember the bird finder program?
In this program, the user queries the program using multiple words and the program searches for the
matching words in its corpus.
OK.
Currently, it's only finds the unique parts and it skips to duplicates.
This time, let's say that the system should only return the first words that it wants and then it will
keep searching for more words altogether.
So even though the user queries the program for multiple words, the system will only find the first
query it worked once, then it will skip the rest.
OK, for example, here it finds the again worked, but it doesn't care about the second part, the
Antwoord.
OK, so how can you add this feature to the program to do that, you need to use a labeled break segment,
then use it.
It will keep it from the whole loop when it finds out.
Let's check it out.
Remember, this got.
The program searches for the correct words in this corpus.
Right, and since that feels here creates a new slice by splitting the words inside the corpus.
And lastly, this corridor will contain security guards.
All right, let me run it first.
With again and answers, as you can see, it finds the unique words inside the corpus.
But now I'm going to only print the first matching words to do that, I need to terminate from the parent
loop when it finds the words.
But this brave statement here terminates from the inner loop, not from the parent, right?
So how can I break from the parent loop?
Of course, I just returned from this function, so the searching will end immediately.
However, sometimes you can't do that and you might want to continue after the loop and so on.
So how can I break from the parent loop without quitting from the program?
To do that, I can use a label statement like this.
I'm going to tie Puri's.
So when I save the file, I've got warns me about that the break label isn't defined.
Let's define it now.
By the way, the position of a label is very important, so I want a break from the parent loop, right?
Then I need to label the parent look like this.
Now the warning has disappeared.
Now these queries here is a label for this apparent loop, so you type a name for it and then you end
it with a column and that's all.
The real power of a label comes from the label statements, not from the label itself.
Here Vandebroek statement is executed, it will break from the parent loop, not from the Knesset loop
anymore.
This is the difference of a label to make statements from a regular break.
It allows you break from any label.
OK, that's right.
As you can see, as soon as it finds the again, it stops searching, right.
This is because the label to break has terminated the loop after it has formed the search.
Let me show you.
Here is label kuris only labels, the first loopier, the parent, the one labeled Break Rense, it
breaks from the first or the parent loop instead of the innermost or the nested loop.
As you know, if the brake statement hasn't been labeled, then it will exit from the nested loop instead
and the search for the other queries could continue.
All right, let's get back.
Let's move the kuris label into the inner loop to see what happens.
Now, the curious label labels the nested loop instead of the parent loop, right?
OK, let's try it.
As you can see now with Prince, all the words instead of the first words.
But here, using a labels break isn't necessary, let me comment off this label and just use an unlabeled
break here like this.
That's right again.
As you can see, the result is the same.
This is because the brake statement already breaks from the inner loop.
So a label isn't necessary.
Here, let me store it.
All right.
Here is label is only meaningful when I label the parent loop.
So when it runs, it quits from the parent loop instead of the nested loop.
By the way, if you don't use a label, Google will warn you because God doesn't let you declare on
labels.
It's like the unused variable error.
For example, let me remove this label here.
As you can see, NALGO says that you have defined the label, but you didn't use it.
Let me do this, all right.
Another rule is that labels do not conflict with the other names they you.
Let's declare a variable name queries here.
As you can see, there isn't a warning, right, because discovery is variable and the label are different
things, even though they have the same names as, you know, normally you can't declare the same name
inside the same block.
Right.
So this means that the labels do not share the same block with the other names like variables, constants
and so on.
But the labels still do have a scope, but it's not a block skop, their scope is actually the whole
body of the function that they've been defining, which is a different scope from a block scope.
That's why Go allows the same names.
As you'll see in the go to statement, that unlike wearables and other names, labels can be defined
even after they've been used.
That's because they belong to the whole body of a function, not just to a block.
Now, let's quickly take a look at how the labeled continua statement works as a.
Now I'm going to show you the labeled continuous statement using the same word finder program.
All those business people, now the requirements are changed.
Now, the system should only bring unique words instead of finding the first match, the words.
So when the user Currys for the again and the Antwerp's, it will print out again and the Antwerp's
once, just like before.
They changed their cameras again.
Sorry about that.
OK, let's take a look at how to do that using a labeled continuous statement.
Let me open the previous program again.
Now, here, instead of the brake statement, I'm going to change it into a continuous statement.
OK.
Here now, this is a labeled continua statement, so it finds the corporate word, it will keep the
current type of dependent and it it'll make it continue from where it left off.
It acts like a usual continuous statement.
The only difference is that this time you can choose where it should continue.
Let me show you how it works.
As you can see, it's only Prince the unique words.
Let me show you how it behaves when I make it an unlabelled continue.
Let's run it now.
As you can see, it prints out the duplicate for now.
Now this continuous statement continues the nested loop instead of the loop.
So when it finds the words, it just continues for the next step of the nested loop by using your labeled
continuum.
I could make it continue the parents loops loop instead of this inner loop.
Let me undo this, OK?
Let's take a look at Antwerp's.
You're this scariest label on the labels, the first loopier, not the NSA.
So here the NSA uses a label to continue Randee continue runs.
It will continue from the parent loop instead of the nest of the.
All right, that's all for now.
This was how a labeled break and labels continue statements.
OK, thank you for watching.
See in the next picture.
Bye bye.