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