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

When should you use a loop in code?

There are a few classic places where loops are used. One of them is when we want to repeat an action another is when we are looking for something and a third is when we want to do something that is mathematical in nature. In this video we explore some of these options.

Video Transcript

00:00Okay, okay. Now I want to ask you a question unless you have another question
00:10Beautiful, so I'm just gonna I opened at the same time
00:13I just opened on the other on my side of the window just going over so we know what a loop is
00:17We already talked about it. You confirmed that you know what a loop is, you know what the same syntax is
00:22We've been talking about the syntax right now as well loop to repeat an action
00:26So first of all before I even do that and you know
00:28Like I should probably hide this because I wanted to ask you that exactly that question
00:31So let me just hide that and I'm gonna ask you that was a question. I wanted to ask
00:36What are loops good for and that's a
00:58That for the first one that's perfect and we've done that so far and that's it
01:01That's completely true, but for loops are a little bit more than that
01:05They're a little bit more than that and I'm gonna give you
01:23Absolutely. So I would say I would say in in in a basic basic scenarios
01:28We said loops are for copying like we said a bureaucratic person just stamping stamps loops are perfect for them
01:33Loops are more than that. Like we said we could say also a loop is good for
01:37Let's say a scenario where there's really really complicated
01:42Mathematics involved where if we'd have to write it ourselves, it'd be really complicated
01:46But in for example, if we wanted to check and I'm just gonna show you this really short sample
01:51So, okay, let me just I'm gonna put the phone down again for a second
01:56Now I want to create a function and I'm going to create a function. Not only I want to
02:00And we're gonna do this together for a second. So I'm gonna create a new function. I'm gonna call this function
02:07powers
02:09We're gonna call it pow for powers
02:11So function powers accepts one parameter which is going to be our the number that's going to be powered and the second number is going
02:18To be another integer and that other integer is going to be the amount of times the power amount
02:23Now this this is going to return an integer as well
02:27Now I want to create a function that I could call pow
02:30I could call the function pow and I'm going to trace it out and I could call it
02:34I want to take the number 2 and and and put it
02:37Powers 4 or in other words, I want to say 2 times 2 times 2 times 2
02:42But I don't I might want to do it this time 2 times 2 times 2
02:45But another time I want to take 3 5 times and I want that flexibility
02:49I don't want to need to type 2 times 2 times 2 4 times each time. This is way more efficient and cleaner
02:55Alright, so how would I write that down? So let's see how we could we create a loop like that
03:00So the first step that I'm going to do I'm going to create a variable and this variable is just going to be my summary
03:04that number I'm going to send back when we're done and
03:07and
03:08The and I'm just going to first create the variable and I'm in the end of it
03:11I'm going to return that variable
03:13So I'm making sure that I'm creating it and I'm returning it now
03:16This next step that I have left is really just to figure out. Okay, how do I do these this powers?
03:20So first of all, I could take into my summary and put into it initially when the initial value in the beginning
03:25I could set its value to be a
03:28Because that's that too. I'm first going to put two inside of it
03:31Now I could start my for loop and I could start my for loop and I could ask a question first of all var I
03:38Which it's going to be an integer. Oops, not uh, whatever that is an integer and
03:45Initially its value is going to be zero, right?
03:47Now what is going to let's see if Carol if you could help me
03:50What would be the question I would ask in this section?
03:52We know that I'm going to add plus one when we're done because we're going to step through them
03:56But what would be the best question to ask for my variable? When when do we know that we're done duplicating?
04:03When I is
04:05Greater than what or equal to?
04:07Then the amount of times the loop has to go right? So two has to go four times
04:18So how many how many what would I have to put it here to make that valid?
04:22Well small it would be because we're starting from zero, right? So zero one two three
04:31So so it actually would be four, right?
04:34No, but because we're sending a variable I could use that variable like that
04:37It doesn't have to be hard-coded like that
04:39I could change this to be 10 if I wanted to right we wouldn't want to hard-code the values
04:43I'm just gonna put here the value B, which is the exact same thing
04:46So as long as I is smaller than V. I want to continue. What do I want to do?
04:50I want to take my summary and I want to duplicate it by how much I want to duplicate it by a
04:56Because I'm taking a and I want to do this four times, right?
05:03So I'm looping in here four times and each time I'm duplicating my summary by a
05:09So the first time it's gonna be two
05:11Times two which would be four and then we go back up times four times two
05:17Then we go back up and then eight times two and then we go back up and then oh it's over
05:22Because it was four. Well, let's make it four. So it's not too long
05:27Does that make sense? If I would write it like this would this be easier
05:37Was this easier to read?
05:50Yeah, and definitely you'll see that you'll all this stuff we've learned so far
05:54We're gonna mix them all together all the time
05:55So we're just taking advantage of the fact that we have a function
05:59So we could use a function and use a loop inside of a function to make our function more dynamic
06:04And that's exactly the step two. So in the beginning we said a loop was for duplicating
06:08But when we look at this loop, this loop is not for duplicating. This loop is for finding things out
06:12It's for creating new things, right? So if we run this application, let me if I ask you how much is three
06:19You know squared not squared but three powers three
06:23Well, it'd be hard to answer that
06:26But if I run it, I could tell you it's 81, but is it really 81?
06:31No, it's not really anyone. Why did it come out to anyone?
06:34Cuz our function is not correct if we run this just one more time. I'm just gonna put your little trays here
06:40trays and
06:42I'm gonna put here the value of sum and
06:46I'm gonna put your oats with the eye just so I could see how the value is growing because I just want to troubleshoot
06:51This is already created this function. Let's make sure that it actually works correctly
06:54So first of all, we'll see that in time zero, it's already worth nine
06:59so that's that's our problem right because it's already a equals three and
07:04Then as soon as we run this it's already when we're still at zero now it equals nine
07:09So because we already put the value three in here
07:12Then we don't want to start from zero. We want to start from one
07:15We want this loop to happen not three times. We want this loop to happen only twice
07:20Does that make sense?
07:26Okay, because because we already this is supposedly instead of doing a loop
07:30We're ready to put the value a inside so ready we did it one time now how many times are left?
07:35We have two more times left. So we want to make sure that this loop will only go
07:39B times minus one
07:42Right or so we could have said if we wanted to we could have we could have set I to be zero and then go
07:50To B and say like as long as I is smaller than B minus one
07:54But it looks a little bit dirtier and it looks like it just looks better if we just set it up to be even though
07:59It's easier to think in that way. It's easier to just set this as one and that solved our problem
08:04Okay. Now we have one more problem though. I want to solve so first of all, how much is three powers one?
08:09Three powers one should equal one. So let's just make sure then when we do three powers one it comes back as one
08:15So I'm gonna save that run it one more time. That's what I'm saying. So three powers one equals three
08:21So that's great that works and the last one that we want to check is we want to make sure that three powers
08:26zero will come back as
08:30That's a good question. I don't really know how much three powers zero comes out. So let's check that
08:34I'm gonna actually use a built-in function to find that answer out. I'm gonna go to the math
08:40Pow, which this is a function that exists. So we just recreated a function that's pre-built in flash
08:46So I'm just gonna ask how much is three times zero. So three zero times should equal one
08:52So for us to be able to solve this we're gonna use the if statement also inside of this function
08:56What we're gonna do is we're gonna ask if B
08:59So we're gonna ask if B
09:03That's all we have to do because if it's zero that means our if statement came back false
09:06So if it's B if it's any value but zero then it will come back as false
09:11If it is true, then we want to put your a if it's not true
09:14we'll just put your one and that solved our problem making sure that our our loot our our powers will also work with the
09:20digit zero and
09:22Then that that even though that's not related directly to for loops
09:25I just wanted to really type that topic and show how do we create that full powers for function?
09:31So we we just and this is a great example because we integrated almost everything we learned in the developer basics so far into this function
09:39All right, so we talked about we talked about duplicating right we talked about
09:44Creating new things were using mathematics. There's one more thing that is done
09:48And I don't I'm not gonna put the example here right now. I think like we'll save this for after you watch the
09:54loops
09:55The final four tier part two and then we'll have another session then because we ready this is this has been a long session
10:02I don't want to overload your brain on this one and and I want to give you a chance not to play around with
10:07This but the third one I'm just gonna say it theoretically. The third one is it enables us to sort so imagine that you have
10:14Imagine you don't have to imagine even in the real world
10:16Let's say you have a haystack and you're looking for a needle now
10:20You could manually yourself pick up every piece of the haystack and search for that needle or you could ask your fun
10:26You could ask your loop to do it for you
10:28And there's a lot of strategies involved with loops that enable you to sort things search for things
10:33And it's loops are very very powerful and they cut down the amount of if you would if you would have to do it
10:39Man, you would have to ask
10:41Every single item every single item using loops. There's smarter ways
10:45For example, if we would type if I would tell you if I would tell you let's say
10:51I have here now a hundred pages and I want and I told you I want you to search for a page that has
10:57The my name in it. There's only one page with my name in it go
11:01That would mean you have to take every single page. But what would happen Carol if the first page you see
11:08Has my name on it already. It's in the title
11:12Now you don't have to check the 99 other pages, right?
11:16Now if I would write things if I would write things in code without loops, I wouldn't know that right
11:22I would have to start typing. I would have to write every single page check page 1 page 2 page 3 page 4
11:27But but you already knew it in page 1 so all the logic from page 2 to page 99 is irrelevant
11:34And that's where loops become also very powerful
11:37So you could search for things with loops and as soon as you find the item that you wanted
11:40You can then terminate your loop because you have your condition as long as that condition is met then and then it's met as soon
11:47As that condition is not made and you could change those conditions inside a loop and that's something we're going to be talking about in
11:51The next hour, but those are the are the three fundamental things that are used for for loops
11:56Obviously, the first two are more common. The third one is more powerful, but with it more complicated. Okay
12:03Okay

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!