Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
You can cause the flow of the loop to change from inside, by breaking and continuing.
-
0:00
Okay, so branching statements.
-
0:02
We talked very briefly about break, Ken you talked about
-
0:05
maybe you're going down the wall and you go woo, this milk's bad now.
-
0:07
You want to break, you want to just get out of this loop.
-
0:09
Get me out of here, I don't want to drink sour milk, the rest of these are all bad.
-
0:12
You can break out of the loop.
-
0:13
It's a keyword.
-
0:14
If you're inside of the loop and you say break, it will stop going.
-
0:18
Continue, we'll look at here in a second, but
-
0:20
what it does is as you're going through each one of the lines of the code there.
-
0:24
If you want to not run the rest of the lines below, you can call continue and
-
0:27
it will jump back to the top and run through again.
-
0:30
Labels are a thing.
-
0:31
You can be inside of a loop and say break, and
-
0:36
somehow they avoided the words saying go to, but you can go to a label.
-
0:40
And through the history of programming, go to is a bad thing.
-
0:43
It creates what is known as spaghetti code.
-
0:45
Which means that you try to follow code and
-
0:47
it looks like little strands of spaghetti.
-
0:50
And it's not a good thing.
-
0:51
You can solve a lot without using a label.
-
0:54
So I say use them sparingly, they're not common.
-
0:56
And if you get into a situation where you want to use one, check yourself.
-
1:00
[LAUGH] You don't want to
-
1:03
introduce spaghetti code.
-
1:04
You don't want somebody looking at your code, trying to be like, I went up here.
-
1:07
Am I over here, where?
-
1:08
You don't want that.
-
1:09
But they do exist, and you might see them.
-
1:11
And you can also return.
-
1:12
If you're inside of a loop, and there's something that you're, like, oh,
-
1:14
this method's done.
-
1:15
I don't need to do this thing at all anymore.
-
1:16
This whole method [INAUDIBLE].
-
1:17
You can just call a return method there, and the loop will stop and
-
1:20
jump out, pop out.
-
1:21
Okay, so the last example that we have is the t-shirt give away.
-
1:24
So, while there's prizes left, we're gonna ask Is the attendee present?
-
1:29
If they're not here, we're gonna try again.
-
1:32
And let's try again here, can anybody think of what that is?
-
1:34
What's this try again?
-
1:35
We're gonna jump back up to the while.
-
1:38
There we go, continue, excellent.
-
1:40
So we're going to continue, we're going to come back up and
-
1:43
say is the attendant present?
-
1:44
If they are here, we're gonna ask what size.
-
1:45
And the reason why we don't want to continue,
-
1:47
we don't want to ask what size if they're not here.
-
1:49
There's no point to do this extra work that's down here,
-
1:51
we're not gonna give away a shirt to nobody either.
-
1:53
So we have to do that.
-
1:53
So what I did was I wrote some JavaScript that [INAUDIBLE] the meetup attendees.
-
1:59
And not 90 people are here, so we might have a continue problem.
-
2:02
We might need to check a continue, but
-
2:04
I am going to randomly sort that list and give out some t-shirts.
-
2:08
That sound good?
-
2:09
All right, you get a shirt, you get a shirt.
-
2:11
>> [LAUGH] >> All right, so
-
2:15
the t-shirt code is in here, called prizes.
-
2:21
Let's bring this down.
-
2:22
So I have a list that you don't need to see, that returns a shuffled
-
2:26
list of all of the Rsvps.shuffled at the time that we run this code.
-
2:29
And I am gonna keep track of the prizes that are given away,
-
2:32
and I am also gonna keep track of the drawingNumber.
-
2:36
And while we've given away five,
-
2:40
we are going to pull out of the RSVP list that number.
-
2:45
This is that for loop.
-
2:46
We could have written this with a for loop, but
-
2:49
notice that I'm using this drawing number.
-
2:51
We're gonna say, Is the person present?
-
2:54
If they're here and I have to remember to type no.
-
3:00
Then we're gonna continue.
-
3:04
So we're calling the continue keyword.
-
3:05
It's gonna go back up to the top.
-
3:06
It's not gonna run the rest of this.
-
3:08
And then we're gonna ask what size you are.
-
3:11
You can kinda imagine multiple bits of this continue or
-
3:14
multiple bits of this break.
-
3:15
This isn't working and there's a break.
-
3:18
Let's imagine that we went past 30 people and nobody's here, we would call break.
-
3:24
We can put in different sorts of things in here.
-
3:25
You could have a continue in here, you could have a break in here,
-
3:27
that sort of thing.
-
3:31
Then we'll do the prizes given away.
-
3:32
So I'm gonna assume that you publicly joined a meetup,
-
3:38
and your name's on the page already.
-
3:40
Your name might be in Treehouse Video, maybe.
-
3:43
So you kind of win two prizes, you win a shirt and Treehouse and
-
3:45
you'll be bummed out if you weren't here, cuz your name's gonna be in a video.
-
3:48
>> And you don't have a shirt.
-
3:50
>> If your name's in the video and you see this at home and you weren't here,
-
3:54
I'll send you a shirt, okay.
-
3:55
>> [LAUGH] >> You're gonna get so busted.
-
3:59
All right, [LAUGH] all right.
-
4:03
Her we go.
-
4:04
Winners, look at winners.
-
4:08
Is Eric Ewer's present?
-
4:11
No Eric Ewers.
-
4:12
So, here we go.
-
4:14
I'm gonna say no and it's gonna continue.
-
4:19
Summer present?
-
4:20
Yeah, all right.
-
4:22
>> [APPLAUSE] >> So I'm gonna say anything.
-
4:24
I'm gonna say, yep, Summer's here, and what's your t-shirt size?
-
4:29
>> Small.
-
4:30
>> Small, okay.
-
4:31
Path's posse, path post.
-
4:36
>> Yeah, that's me.
-
4:37
>> That's you?
-
4:38
Yeah, all right, so we'll loop here till we hit five.
-
4:43
All right.
-
4:48
I'm not actually gonna use this data we're just,
-
4:50
I was kind of trying to show that we did the.
-
4:52
Sarah I know her, and she is not here, Sarah.
-
4:57
>> [LAUGH] >> Is Amy present?
-
4:59
All right.
-
5:00
>> [APPLAUSE] >> Ashley.
-
5:07
>> [APPLAUSE] >> All right,
-
5:12
Simone Flore?
-
5:16
No, Simone.
-
5:17
>> [LAUGH] >> Yeah.
-
5:20
>> [APPLAUSE] >> There we go.
You need to sign up for Treehouse in order to download course files.
Sign up