- 3 days ago
Category
😹
FunTranscript
00:00In this code.org lesson, we're going to focus on the order that code runs, the sequence
00:21of it.
00:22Let's get going.
00:23This is code.org.
00:25Let's see what we're doing.
00:26Alright.
00:27Okay, so it looks like we're just going forward.
00:31We can do that.
00:32I'm just going to droop and droop.
00:34How many times?
00:35One, two, three, four, five.
00:38So I'm going to attach all of my blocks to the win run block.
00:40One, two, three, four, five.
00:43There we are.
00:44Now there's probably a faster way to do this, but let's see.
00:47This should work just fine.
00:48Boom, boom, boom, and coom.
00:52Now if you want to get fancy, let me reset.
00:55This works as well, boom, and it's shorter.
00:58It's more efficient, and we really should think about that.
01:01Everything in this pink mouth thing will now run five times.
01:03So we move it forward, hit the bottom, zoop, five times in a row.
01:08Less code, same result.
01:10Awesome.
01:11Onward.
01:12Alright, we have one block on our board.
01:15We can use up to nine.
01:16Use turn left block to get the acorn.
01:19So I'm going to just throw some code down here and see if it works.
01:22You should try the same.
01:25So I wasn't super sure what to use.
01:27I'm just testing stuff out.
01:29You should always do this.
01:30You can be wrong with code.
01:31It's okay.
01:32Uh-oh.
01:33Oh, no.
01:36I can also use step to watch my code run.
01:38See, move forward, and that way I catch errors much more quickly than leaving them there and
01:43not finding them until the end, or having a ton of code that I have to kind of search through
01:48for an error.
01:49Let's see.
01:50So I've got to go forward a bunch and then left.
01:53Let's try.
01:54Boom.
01:55Now, this totally works.
01:57It's completely right.
01:58But once again, let's take a look.
01:59I have nine blocks here, but I'm repeating myself.
02:02One, two, three, four, five.
02:05So a better way to do that is to use a repeat block.
02:08What I had was technically correct, but this is more efficient.
02:11So what this will do now, everything in this pink mouth loop thing block will run five times
02:17I'm asking it.
02:18So it will move forward, back to the top, move forward, three more times, move forward,
02:22two more times, move forward, one more time, move forward.
02:24Now it's done.
02:25Once it's done all the numbers of times, it just falls beneath and goes turn left, forward,
02:30forward.
02:32Awesome.
02:33Onward.
02:34Video.
02:35Let's keep going.
02:37All right.
02:38Can you get to the acorn with just one move forward?
02:41I bet we can.
02:43Ooh.
02:44Okay.
02:45So now we have one move forward we're allowed to use.
02:47I'm going to start how I often do.
02:49I'm going to guess and throw down code.
02:51I'm going to use too many blocks, right?
02:52I already have two, but I'm not sure.
02:55And it is okay to do this, to test stuff out.
02:58So look at all of these.
02:59I'm going to actually use step this time.
03:00Boom.
03:01Okay.
03:02One step, two steps, and I can watch it run.
03:04Three steps, four steps.
03:06This helps me.
03:07Okay.
03:08So we're going to get there.
03:09If I hit step five, we get the acorn.
03:10But I have too many blocks.
03:11So I won't get like full credit.
03:13It only goes half green.
03:15So instead of doing it this way, one, two, three, four, five, boom.
03:23This is the same thing, except everything in this pink mouth thing will now run five times
03:28in a row, just like we've been using it.
03:30Three blocks.
03:31Three blocks.
03:32Awesome.
03:33Onward.
03:34Onward.
03:35Add one more block to get scrapped all the way to the acorn.
03:38Oh, five out of six.
03:39Oh, so what's happening here?
03:40We already have a loop.
03:42So this first loop, let's watch it run.
03:43I'm going to hit step and oh, it's annoyed because there's nothing down here.
03:49I'm going to detach that just to show you what's going on so far.
03:52Step.
03:53So I'll move forward.
03:54It hits the bottom of our pink block and it's going to go back to the top because we have
03:58to do this four times.
03:59We've only done it once.
04:00Second time.
04:01Move forward.
04:02And it will stay highlighted because we're going to go forward a third time.
04:05It stays highlighted because a fourth time.
04:08And now it's just going to drop beneath and run turn right because we've done this four
04:12times.
04:13Turns right.
04:14Okay.
04:15So what would we need to do next?
04:16Well, let's see.
04:17One, two, three, four steps.
04:19Wait a minute.
04:20Boom.
04:21And I can have this right here.
04:23Because again, I can do four move forwards, turn right, and then move forwards.
04:30Only the stuff in these pink areas will get repeated.
04:32So this gets repeated four times.
04:34I turn right once and then I should move forward four times.
04:37Let's check.
04:38Victory onward.
04:42Can you add a loop to get to the acorn?
04:45Ooh.
04:46So now we have, let's see what it does so far.
04:48We should always check.
04:50Oh, okay.
04:52Okay.
04:53So we have a repeat four and then I turn right once.
04:55Then I repeat four.
04:56Then I turn right once.
04:57Now, what am I repeating?
04:59Well, four times in a row I move forward because that's what's in this pink mouth thing.
05:02So move forward once.
05:03Move forward twice.
05:04Three times.
05:05Four times.
05:06Once it gets to four, right?
05:08Because this says do this four times.
05:10It falls beneath the pink block and I turn right once.
05:13Plop.
05:14Then I hit this again.
05:15It says, oh, okay.
05:16Four times.
05:17Forward once.
05:18Plop.
05:19Hits the bottom of this pink block.
05:20Back to the top.
05:21Forward twice.
05:22Hits the bottom of this pink block again.
05:24Back to the top.
05:25Forward three times.
05:26Plop.
05:27Hits the bottom.
05:28Forward four times.
05:29And here I am.
05:30So now what do I need to do?
05:31Well, I turn right, right?
05:33So we move forward four times and now it falls beneath because it's done.
05:37Turn right.
05:38Now what?
05:39I bet if I'm here.
05:41Oops.
05:42What do I got to do?
05:43I bet I need to move forward four more times.
05:47Victory.
05:48Onward.
05:49Ooh, Scrap really wants that acorn.
05:52So I have one block and I can use nine up to three and move forward.
05:55So let's try it because we've been using repeat.
05:58Let's see if I can go forward four times.
06:00One, two, three, four.
06:02And then I'm going to try to turn right.
06:05And just to test this, I'll move a move forward.
06:07I'll throw a move forward here.
06:08So step.
06:09Now what should happen?
06:10Yep.
06:11This should be yellow.
06:12Now I need to repeat this four times.
06:13Everything in this pink mouth.
06:14So once I hit step again, it hits the bottom of my pink loop, goes back to the top and says
06:18right three more times and moves forward.
06:20Zoop.
06:21And so that stays highlighted.
06:22And then again, hits the bottom two more times.
06:25Hits the bottom.
06:27Back to the top and I should move forward one more time.
06:30Perfect.
06:31Now it's done with all four.
06:32It's going to fall beneath just like I've been saying.
06:34And we should turn right once now because this isn't in a pink block.
06:37Boom.
06:38Now I have moved forward out of a pink block.
06:40We'll step.
06:41But what do I need?
06:42Well, I need to go forward four times here.
06:44So let's do what we have been.
06:47Boom.
06:48Now I only, after I go forward four times, I'd be here.
06:51What do I need to do?
06:52Well, I need to turn right.
06:53Boop.
06:54I hope you're yelling at me though, because we don't want to turn right every time we move
06:59forward.
07:00We don't want to turn right in this pink loop.
07:02That would mean every time we go forward, we have to turn right.
07:05No, we only want to turn right after we move forward four times.
07:09That gets us here.
07:10Finally, what are we going to do once we turn right?
07:13Hmm.
07:14Looks like four more times.
07:16If this isn't clicking, definitely go back and watch the tutorial for the last level as
07:20well.
07:21This stuff is tricky, but super useful.
07:24Victory.
07:25Onward.
07:26Uh-oh.
07:27Can we fix it?
07:28I don't know.
07:29Let's see.
07:30Ooh.
07:31Uh-oh.
07:32Goodbye.
07:33Okay.
07:34So what's going on here?
07:36Well, everything in the pink loop gets repeated.
07:39So I know we'd move forward three times.
07:42Let me step through this to check.
07:44Move forward once.
07:46Twice.
07:47Zoop.
07:48One more time.
07:49Three times.
07:50So now I'm done, right?
07:51We've done this stuff in the pink mouth, pink box, pink loop thing three times.
07:55And so now it should fall beneath and I should turn left.
07:58Awesome.
07:59Okay.
08:00Now what?
08:01Well, I only have to turn left once.
08:02It's not in a pink box, right?
08:03It's outside of them.
08:04So what happens after I turn left?
08:05Zoop.
08:06All right.
08:07Ask the computer, do this three times.
08:08Okay.
08:09Move forward once.
08:10Hits the bottom of our pink loop.
08:12Zoop.
08:13I should move forward again.
08:14Yep.
08:15Hits the bottom.
08:16One more time to hit three.
08:17Yep.
08:18Now I've done this three times.
08:19So now I'm going to fall beneath and turn left.
08:20What?
08:21Oh.
08:22Now what am I about to do?
08:23Now I'm about to go forward four times.
08:25That doesn't work.
08:26So if it's not three, four, let's try five here.
08:31And let's see.
08:32Four, one, two, three, four.
08:35That looks good.
08:36One, two, three, four.
08:37Let's see what we got.
08:40Yeah.
08:41Champions onward.
08:44The acorn is so far away.
08:46Can you use loops to help?
08:47Yep.
08:48Okay.
08:49So I have one block I can use up to 12.
08:50I'm just going to throw code down.
08:52It says we can only use move forwards.
08:54I think you should do the same.
08:55Just start putting code down to test it.
08:57So I'll put a bunch of stuff down and then talk about it.
09:00So this gets us there but we have way, way too much code.
09:09But you should try to do this often, right?
09:11It helps me be able to see what's going on without being too tricky.
09:16Especially with loops it gets confusing.
09:18Okay.
09:19So right now I have 20 blocks and I can only use 12.
09:21Let's look for a repeat of code.
09:22I hope you're already yelling at me because obviously saying forward one, two, five times
09:28in a row is ridiculous.
09:29Droop and droop.
09:30Let's be lazy.
09:31Bam.
09:32That will do the same thing.
09:34Okay.
09:35And then what else?
09:37Oh, wait a minute.
09:38What do we do again?
09:39One, two, three, forward five times in a row.
09:42We don't need to do that.
09:44Droop and droop.
09:45Goodbye.
09:46This will do that automatically.
09:47All right.
09:48Oh, wait.
09:49Forward three times in a row.
09:51Nope.
09:52We don't need to do that either.
09:54We can do it this way.
09:55Let me change that to three.
09:57And it kind of gives us a hint by only giving us four move forward blocks.
10:01Well, look.
10:02One, two, three, four sides.
10:04Forward three times in a row.
10:06We'll wait.
10:07First turn right and then forward three times.
10:10Boom.
10:11It's just different lengths of sides, but similar to what we were just doing.
10:15And that we loop through a certain number of times, move forward.
10:18Afterwards, we turn once, which is why it's not in the pink block because only the code
10:23in the pink block repeats.
10:25Once it completes these five move fours, we drop beneath this, turn right once.
10:29It's not in pink.
10:30Then we go ahead and we turn right here, right?
10:33So then we move forward five more times and up here.
10:37Once we've done move forward five times, we fall beneath and turn right.
10:40So on.
10:41Champion.
10:42Ooh, challenge.
10:43We are completely ready.
10:45All right.
10:46So we can use up to 15 blocks.
10:47Here's our acorn.
10:48I'm going to do what I often do.
10:49I'm going to throw down way too much code.
10:50It says I'm going to use seven.
10:51I'm going to use like 20.
10:52Because I just want to see what the code does.
10:53So you give this a shot too.
10:54I'm going to throw down a bunch of stuff and then talk about it.
10:57And I'm already at 15 blocks, but I'm just testing.
10:58Whoop.
10:59And that's why we test.
11:00Wow.
11:01And we're still not there.
11:02So we're getting closer, but it looks like this would be another five.
11:04Obviously, I have way too much code here, but it's a really great way to find our loop.
11:05So I have 24 out of 15.
11:06Well, first off, move forward five times in a row.
11:07Nah.
11:08We can use this, right?
11:09Instead, let's put it in this pink block.
11:10Let's have our repeat be five times.
11:11And let's go ahead and do that.
11:12And I'm already at 15 blocks, but I'm just testing.
11:13Woop.
11:14And that's why we test.
11:15And that's why we test.
11:18Wow.
11:19And we're still not there.
11:20So we're getting closer, but it looks like this would be another five.
11:23Obviously, I have way too much code here, but it's a really great way to find our
11:28loop.
11:29So I have 24 out of 15.
11:30Well, first off, move forward five times in a row.
11:31Nah.
11:32We can use this, right?
11:33Instead, let's put it in this pink block.
11:35Let's have our repeat be five times.
11:38And now that will automatically go move forward five times.
11:41Once it's done with this move forward five times,
11:44it drops below and will turn right once.
11:46So it's identical.
11:47Now we have move forward twice.
11:49We could use a loop there.
11:50We also have move forward one, two, three, four, five again.
11:54So again, move forward five times.
11:57Doing this is absolutely identical.
11:59Putting a five in this repeat, putting move forward,
12:02completely the same thing.
12:05There we are.
12:06Now what?
12:07Turn left, move forward twice, okay?
12:09And then move forward five more times.
12:10Boop.
12:11Boop.
12:14Looks good.
12:15If you really wanted to, it's going to count for the same amount of blocks.
12:18At least it should.
12:19You could also do move forwards here.
12:23Right?
12:23Like that.
12:24But it won't make a difference because it's the same amount of blocks.
12:27Two move forwards or a repeat.
12:29Boom.
12:30Let's give this a shot.
12:34Victory.
12:34Three.
12:36That one is tricky.
12:40Onward.
12:43We got one block.
12:44We can use up to 12.
12:45Scrat needs this acorn.
12:46We can use six move forwards.
12:48I'm going to start with what I often do, throwing down too much code and testing it.
12:51You try that too.
12:52I can already tell I'm over on code, but again, just testing.
12:57Let's see.
13:00Whoops.
13:01Well, I killed the scratch.
13:03Okay.
13:03Boom.
13:04But it looks like four here.
13:06So let me actually, before I even continue, I don't need four in a row.
13:09We know.
13:10Boom.
13:11Boom.
13:11Let's try that.
13:12And then I bet this will be four or two down here, so I can already do.
13:19We know if I use a repeat, it will do it for us four times.
13:22Once I'm here, though, would I want to go left?
13:25Let's try this.
13:26And maybe a move forward and a move forward.
13:29So four times in a row, we'll go forward.
13:31Once we're done going four times, we fall beneath, turn right, move forward once, twice,
13:36turn right once, right, because everything not in pink happens once.
13:40We move forward once, we move forward again, because they're following each other, not
13:43because they're in pink.
13:44Then we'll turn right.
13:45Then four times in a row, one, two, three, four.
13:48Once we're done with this four times, we hit this.
13:50We should turn left once.
13:51It's not in the pink.
13:52And then move forward twice.
13:55Champions onward.
14:00Ooh, get to the acorn any way we can, and we have infinity blocks.
14:04Well, with how much we've been looping, one, two, three, four, five, I'm going to use loops,
14:10because it seems easy now.
14:13So I should go up five, and then since I only want to turn right once, I'll put that here.
14:19Let me test these.
14:21Oh, didn't even think.
14:23If I wanted to go up first, this is fine, though.
14:26Let's just have me turn left.
14:28Whoops!
14:30Great.
14:31Now I need to move one, two, three, four, five, forward.
14:34Boom.
14:36Six blocks.
14:38There could even be a faster way.
14:41Yeah!
14:42So that works.
14:44I want to show you even a faster way.
14:46Let's say at the very end right here we turn left.
14:49Would it matter if I wanted to turn left one more time?
14:52It wouldn't.
14:53So what I had was correct, and it works.
14:55But it wouldn't, right?
14:56Well, wait a minute.
14:57These are now identical.
14:58Don't repeat yourself in code.
15:00So if we wanted to get real fancy, this is called a nested loop.
15:04I'll ask it to run everything in this pink mouth thing twice.
15:08So it's a repeat block with a repeat block inside of it.
15:12Two times over, we'll move forward.
15:14First, we move forward five times.
15:16Get to here.
15:17We'll turn left once.
15:19We hit the bottom of this.
15:20Back to the top.
15:21Oh yeah, one more time.
15:22So then we'd move forward five times.
15:24Once we're done that, get to the bottom.
15:26We turn left.
15:26But we already completed.
15:27Either way is correct.
15:31Let's keep going.
15:37An if-else statement is a decision between two things.
15:41So say for example, if Scrat finds the acorn, he's happy.
15:45Else, he's sad and keeps searching.
15:48Now let's see how we can use an if-else statement with our buddy Scrat.
15:51This block looks the same as the if-block, but there's an extra part on the bottom that says else.
15:58If I put a move block where it says do, and a turn left block where it says else,
16:03that means Scrat will move forward if there's a path ahead.
16:07And if there's not a path ahead, Scrat will turn left.
16:10It's making a decision and doing one of the two actions based on that decision.
16:15And just like if-blocks, you can put if-else blocks inside repeat blocks.
16:19Now let's help Scrat get that acorn.
Recommended
7:34
|
Up next
6:20
14:52
0:55
1:49
2:09
0:45
Be the first to comment