1 00:00:00,300 --> 00:00:02,630 Okay, so branching statements. 2 00:00:02,630 --> 00:00:05,150 We talked very briefly about break, Ken you talked about 3 00:00:05,150 --> 00:00:07,890 maybe you're going down the wall and you go woo, this milk's bad now. 4 00:00:07,890 --> 00:00:09,720 You want to break, you want to just get out of this loop. 5 00:00:09,720 --> 00:00:12,090 Get me out of here, I don't want to drink sour milk, the rest of these are all bad. 6 00:00:12,090 --> 00:00:13,400 You can break out of the loop. 7 00:00:13,400 --> 00:00:14,010 It's a keyword. 8 00:00:14,010 --> 00:00:18,070 If you're inside of the loop and you say break, it will stop going. 9 00:00:18,070 --> 00:00:20,413 Continue, we'll look at here in a second, but 10 00:00:20,413 --> 00:00:24,289 what it does is as you're going through each one of the lines of the code there. 11 00:00:24,289 --> 00:00:27,699 If you want to not run the rest of the lines below, you can call continue and 12 00:00:27,699 --> 00:00:30,009 it will jump back to the top and run through again. 13 00:00:30,009 --> 00:00:31,930 Labels are a thing. 14 00:00:31,930 --> 00:00:36,540 You can be inside of a loop and say break, and 15 00:00:36,540 --> 00:00:40,280 somehow they avoided the words saying go to, but you can go to a label. 16 00:00:40,280 --> 00:00:43,630 And through the history of programming, go to is a bad thing. 17 00:00:43,630 --> 00:00:45,830 It creates what is known as spaghetti code. 18 00:00:45,830 --> 00:00:47,360 Which means that you try to follow code and 19 00:00:47,360 --> 00:00:50,230 it looks like little strands of spaghetti. 20 00:00:50,230 --> 00:00:51,980 And it's not a good thing. 21 00:00:51,980 --> 00:00:54,010 You can solve a lot without using a label. 22 00:00:54,010 --> 00:00:56,250 So I say use them sparingly, they're not common. 23 00:00:56,250 --> 00:01:00,565 And if you get into a situation where you want to use one, check yourself. 24 00:01:00,565 --> 00:01:03,730 [LAUGH] You don't want to 25 00:01:03,730 --> 00:01:04,400 introduce spaghetti code. 26 00:01:04,400 --> 00:01:07,110 You don't want somebody looking at your code, trying to be like, I went up here. 27 00:01:07,110 --> 00:01:08,050 Am I over here, where? 28 00:01:08,050 --> 00:01:09,130 You don't want that. 29 00:01:09,130 --> 00:01:11,210 But they do exist, and you might see them. 30 00:01:11,210 --> 00:01:12,060 And you can also return. 31 00:01:12,060 --> 00:01:14,450 If you're inside of a loop, and there's something that you're, like, oh, 32 00:01:14,450 --> 00:01:15,400 this method's done. 33 00:01:15,400 --> 00:01:16,708 I don't need to do this thing at all anymore. 34 00:01:16,708 --> 00:01:17,530 This whole method [INAUDIBLE]. 35 00:01:17,530 --> 00:01:20,524 You can just call a return method there, and the loop will stop and 36 00:01:20,524 --> 00:01:21,980 jump out, pop out. 37 00:01:21,980 --> 00:01:24,858 Okay, so the last example that we have is the t-shirt give away. 38 00:01:24,858 --> 00:01:29,978 So, while there's prizes left, we're gonna ask Is the attendee present? 39 00:01:29,978 --> 00:01:32,039 If they're not here, we're gonna try again. 40 00:01:32,039 --> 00:01:34,970 And let's try again here, can anybody think of what that is? 41 00:01:34,970 --> 00:01:35,780 What's this try again? 42 00:01:35,780 --> 00:01:37,419 We're gonna jump back up to the while. 43 00:01:38,560 --> 00:01:40,700 There we go, continue, excellent. 44 00:01:40,700 --> 00:01:43,084 So we're going to continue, we're going to come back up and 45 00:01:43,084 --> 00:01:44,318 say is the attendant present? 46 00:01:44,318 --> 00:01:45,958 If they are here, we're gonna ask what size. 47 00:01:45,958 --> 00:01:47,568 And the reason why we don't want to continue, 48 00:01:47,568 --> 00:01:49,660 we don't want to ask what size if they're not here. 49 00:01:49,660 --> 00:01:51,390 There's no point to do this extra work that's down here, 50 00:01:51,390 --> 00:01:53,050 we're not gonna give away a shirt to nobody either. 51 00:01:53,050 --> 00:01:53,790 So we have to do that. 52 00:01:53,790 --> 00:01:59,570 So what I did was I wrote some JavaScript that [INAUDIBLE] the meetup attendees. 53 00:01:59,570 --> 00:02:02,600 And not 90 people are here, so we might have a continue problem. 54 00:02:02,600 --> 00:02:04,240 We might need to check a continue, but 55 00:02:04,240 --> 00:02:08,820 I am going to randomly sort that list and give out some t-shirts. 56 00:02:08,820 --> 00:02:09,449 That sound good? 57 00:02:09,449 --> 00:02:11,108 All right, you get a shirt, you get a shirt. 58 00:02:11,108 --> 00:02:15,269 >> [LAUGH] >> All right, so 59 00:02:15,269 --> 00:02:18,660 the t-shirt code is in here, called prizes. 60 00:02:21,140 --> 00:02:22,349 Let's bring this down. 61 00:02:22,349 --> 00:02:26,045 So I have a list that you don't need to see, that returns a shuffled 62 00:02:26,045 --> 00:02:29,820 list of all of the Rsvps.shuffled at the time that we run this code. 63 00:02:29,820 --> 00:02:32,886 And I am gonna keep track of the prizes that are given away, 64 00:02:32,886 --> 00:02:36,370 and I am also gonna keep track of the drawingNumber. 65 00:02:36,370 --> 00:02:40,101 And while we've given away five, 66 00:02:40,101 --> 00:02:45,769 we are going to pull out of the RSVP list that number. 67 00:02:45,769 --> 00:02:46,809 This is that for loop. 68 00:02:46,809 --> 00:02:49,151 We could have written this with a for loop, but 69 00:02:49,151 --> 00:02:51,448 notice that I'm using this drawing number. 70 00:02:51,448 --> 00:02:54,268 We're gonna say, Is the person present? 71 00:02:54,268 --> 00:03:00,547 If they're here and I have to remember to type no. 72 00:03:00,547 --> 00:03:04,247 Then we're gonna continue. 73 00:03:04,247 --> 00:03:05,587 So we're calling the continue keyword. 74 00:03:05,587 --> 00:03:06,766 It's gonna go back up to the top. 75 00:03:06,766 --> 00:03:08,790 It's not gonna run the rest of this. 76 00:03:08,790 --> 00:03:11,160 And then we're gonna ask what size you are. 77 00:03:11,160 --> 00:03:14,070 You can kinda imagine multiple bits of this continue or 78 00:03:14,070 --> 00:03:15,790 multiple bits of this break. 79 00:03:15,790 --> 00:03:17,650 This isn't working and there's a break. 80 00:03:18,800 --> 00:03:24,150 Let's imagine that we went past 30 people and nobody's here, we would call break. 81 00:03:24,150 --> 00:03:25,850 We can put in different sorts of things in here. 82 00:03:25,850 --> 00:03:27,740 You could have a continue in here, you could have a break in here, 83 00:03:27,740 --> 00:03:31,000 that sort of thing. 84 00:03:31,000 --> 00:03:32,990 Then we'll do the prizes given away. 85 00:03:32,990 --> 00:03:38,520 So I'm gonna assume that you publicly joined a meetup, 86 00:03:38,520 --> 00:03:40,880 and your name's on the page already. 87 00:03:40,880 --> 00:03:43,360 Your name might be in Treehouse Video, maybe. 88 00:03:43,360 --> 00:03:45,844 So you kind of win two prizes, you win a shirt and Treehouse and 89 00:03:45,844 --> 00:03:48,728 you'll be bummed out if you weren't here, cuz your name's gonna be in a video. 90 00:03:48,728 --> 00:03:50,949 >> And you don't have a shirt. 91 00:03:50,949 --> 00:03:54,359 >> If your name's in the video and you see this at home and you weren't here, 92 00:03:54,359 --> 00:03:55,789 I'll send you a shirt, okay. 93 00:03:55,789 --> 00:03:59,228 >> [LAUGH] >> You're gonna get so busted. 94 00:03:59,228 --> 00:04:03,700 All right, [LAUGH] all right. 95 00:04:03,700 --> 00:04:04,560 Her we go. 96 00:04:04,560 --> 00:04:05,608 Winners, look at winners. 97 00:04:08,688 --> 00:04:10,430 Is Eric Ewer's present? 98 00:04:11,870 --> 00:04:12,588 No Eric Ewers. 99 00:04:12,588 --> 00:04:14,609 So, here we go. 100 00:04:14,609 --> 00:04:17,148 I'm gonna say no and it's gonna continue. 101 00:04:19,249 --> 00:04:20,788 Summer present? 102 00:04:20,788 --> 00:04:22,309 Yeah, all right. 103 00:04:22,309 --> 00:04:24,899 >> [APPLAUSE] >> So I'm gonna say anything. 104 00:04:24,899 --> 00:04:29,759 I'm gonna say, yep, Summer's here, and what's your t-shirt size? 105 00:04:29,759 --> 00:04:30,579 >> Small. 106 00:04:30,579 --> 00:04:31,939 >> Small, okay. 107 00:04:31,939 --> 00:04:36,578 Path's posse, path post. 108 00:04:36,578 --> 00:04:37,938 >> Yeah, that's me. 109 00:04:37,938 --> 00:04:38,599 >> That's you? 110 00:04:38,599 --> 00:04:43,658 Yeah, all right, so we'll loop here till we hit five. 111 00:04:43,658 --> 00:04:44,578 All right. 112 00:04:48,539 --> 00:04:50,568 I'm not actually gonna use this data we're just, 113 00:04:50,568 --> 00:04:52,278 I was kind of trying to show that we did the. 114 00:04:52,278 --> 00:04:57,735 Sarah I know her, and she is not here, Sarah. 115 00:04:57,735 --> 00:04:59,928 >> [LAUGH] >> Is Amy present? 116 00:04:59,928 --> 00:05:00,829 All right. 117 00:05:00,829 --> 00:05:07,153 >> [APPLAUSE] >> Ashley. 118 00:05:07,153 --> 00:05:12,513 >> [APPLAUSE] >> All right, 119 00:05:12,513 --> 00:05:16,233 Simone Flore? 120 00:05:16,233 --> 00:05:17,693 No, Simone. 121 00:05:17,693 --> 00:05:20,513 >> [LAUGH] >> Yeah. 122 00:05:20,513 --> 00:05:27,272 >> [APPLAUSE] >> There we go.