02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Continue in Action

A sample of working with continue.

Video Transcript

00:01We've seen the continue command, but so far we haven't really seen a practical use for it
00:06So let's try and find a practical use and even though you'll probably find out as you work in the visual display
00:13You're gonna find that the continues are gonna be much more useful in those scenarios
00:17But let's imagine that we have five players right now playing a game and in the rules of this game whenever a user
00:24Makes a felony all the rest of the players receive a point
00:28But they don't receive the point themselves. All right, so that's basically the rule of what we're trying to try to do
00:33So before we do that, let's just output the scores of our users
00:37So I'm just gonna do a for loop and we take a var
00:41Value well var key
00:45Which would be a string inside of o players and then I'm just gonna trace out my key and
00:54My value which would be o players
00:58Open bracket key close bracket
01:01All right
01:02So if I run this it should display been zero Dan zero and obviously the order is not really important
01:08So the order would be random, but the values is what we're really concerned about
01:14Now if we wanted to we could have added also some tabs like we did in one previous example
01:19So we could slash T to create a tab and then we'll make it line up a little bit nicer
01:24All right, so we got a nice layout
01:26Let's build out now the function that actually changes the scores of users
01:30So I'm gonna create another private function and I'm gonna call it
01:34Update score update score will receive a list of players
01:39Which is an object and it's gonna receive a
01:43Key that is going to ignore. So let's call it s ignore
01:48Which would be a string and s ignore means that ignore that user don't add a point to that user
01:55All right. So let's see how we would build that function
01:57So the function would basically be very similar to the function that we have right here, right?
02:02So I'm just going to duplicate that function and just change the value inside
02:06So what do we want to test first? We want to see if our key
02:10If our key equals our s ignore
02:15What do we want to do? We want to continue want to get out of that loop?
02:19Otherwise we'll do all the executable code that we want to do in this scenario. It's a very simple code, which is just basically Oh
02:27players
02:28key
02:31plus plus
02:34Now if we want to update our score and test it we're going to do it before our for loop that we're running through
02:38So we're just going to update the score of our Oh players and we're gonna ignore
02:44Ben
02:46Let's do that a couple of times. So I'm going to call update score three times
02:50Ignoring Ben and all three and let's one time ignore Sam and let's see what our output will be if we do that
02:56Oh, we have a little error here. Let's see what it is
02:59So we're accessing an undefined variable, which is true because we're trying to access Oh players when we create it
03:05We pass through a variable called players, right? And again, that's related to the scope issues
03:10So Oh player belongs to the scope of our main right? It belongs to this
03:16function and
03:17on the other hand on the other hand when we're inside of the update score the players is
03:25What we have available. We don't have a player Oh players available
03:29So we have to change players in line 27 and in line 30. All right, let's run this one more time
03:35And here we go. And now we're getting the scores exactly as we would hope to get because it ran four times
03:41We never we never disabled Dan Lind or Albus. So they all got four points Sam
03:47We disabled ones and then we disabled in three out of the four options
03:52All right
03:52so it's not a very practical example because it's really hard to find practical examples when you're not working with the visual displays, but
04:01We might see something interesting in the very shortly
04:05But this is basically continue and you probably won't need to use it in most scenarios because you could have just set here
04:11If not and just wrap your if value inside of it, right?
04:15So continue most of the time you could find a way around using continue besides some very very
04:23very very not common points where knowing this knowing how to use continue might be useful in very
04:31specific scenarios

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!