Labels
The final topic to wrap up our Developer basics. Its now and relatively unknown but a very cool feature and a great way to wrap up working with loops. in this title we will see how we can tag a loop and give it a name and then revisit our prime numbers sample and modify it to work with a tagged for loop.
Video Transcript
00:00Our last topic in the basic topics of loops is labels, which was introduced in action script 3
00:08And it's very similar to if you work from Visual Basic or some other languages, which they call it go to
00:14Let's see what that means. What is the label? A label in action script 3 enables you to give your for loop a name
00:21And why would you want to give your for loop a name and for that?
00:24I just paste it inside one of our earlier examples of a nested loop where we looped
00:30inside of a loop to find prime numbers
00:33And we had to use an external variable is prime and change it around to make sure that when we get out of our for loop
00:40We still know it's a prime number and the reason was was because we could only break out of our for loop ones
00:46We couldn't break twice because as soon as we broke out, that's it. We're out of that loop
00:51What if we had a way to tell flash to break out of the external loop and that's exactly where labels come in
00:57So let's see how that's done to give a label to your to your for loop
01:01We're gonna do it only to our external for loop. I'm just gonna call my external for loop
01:06external
01:07loop and then colon and there we go
01:11So that gave my for loop a name and now that my for loop has a name
01:15I could actually go to my break and break out of my for loop
01:21Alright, I could do I could use break and I could use continue now break is probably not the best thing to use here
01:27Right. So let's change around our code for a second
01:30First of all, we don't need our variable anymore. So I'm just gonna get rid of it. We don't need our is prime anymore
01:36And we don't need to check for it
01:38Now let's let's start going through the logic now if our number is
01:44Divisible right if our number is divisible, then what does that mean?
01:47That means we are we don't want to output this trace, right?
01:50So we want to break out but break what does break do as we've seen before break actually terminates a loop
01:56Thus if we say break external loop, it would actually turn them it turn it would
02:02It would finish the loop thus we would never even get any we wouldn't get our loops. We only would run it one time
02:09Okay, because as soon as we would hit break, that's it. It was our final loop. We're not gonna loop through this again
02:16So that isn't really what we want, right? We want to continue so although we could break out
02:21We don't really want to in this scenario. We want to continue because continue as we've seen in the last video
02:28Enables us to go to the loop and continue it
02:31So basically break out of a current loop cycle and move on to the next one
02:35That's hitting the continual exit my for loop and start over my loop
02:41All right, and let's run it again. Now if we run it with a continue
02:45We should be getting all of our prime numbers now and our code is much smaller
02:49and now if we wanted to and we wanted to clean up our code even more because we're using only one line some developers would
02:56Probably just get rid of all
02:58the extra lines
03:01But we won't because we said we want to keep our code legible and easy to see what's going on
03:06right, but this for my in my eyes is legible, but many developers would get confused by it because
03:13developers might think oh well it might maybe this is part of this loop and
03:18it might be confusing because for loops we said only have one line, but we have your two lines and
03:23Because this is not really a line until we have our semicolon. This is just the beginning of the line, so
03:28To keep our code simple and legible for users. We're just going to keep it the way it was previously
03:34so
03:35And here we go. We're missing one more bracket and there we go. So this is how we use
03:41Labels labels are very powerful and very useful and again
03:44One of those features that you use here and there and not daily
