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

The wild while loop

Efficiency is important in some languages while less so in others. When you're running a program in PHP you have only one computer that is serving hundreds of people. Therefore, it’s important that each line of code is as efficient as it can be. Contrary to that, client-side programming languages such as JavaScript don't need to be as efficient. Since you only have one computer running the script at a time. That's why I personally prefer doing what's more comfortable and not necessarily what's more efficient on client-side programming languages.

Note: The for loop isn't as efficient as The while loop. If you are an inefficiency geek, you will want to stick to while loops.

Video Transcript

00:00Basically, the wild loop is a more primitive for loop.
00:06It's not one that I really recommend you use on a daily basis, but it's important
00:09for you to be familiar with it and know how to work with it.
00:13Let's see what I can tell you.
00:14So far we've seen the for loop and really all the loops are very similar.
00:18To be honest, this is the best loop.
00:20It's not the most efficient, but it's the easiest one.
01:15And I personally recommend just use this one, but I want to show you the other ones
01:19just so you're familiar with them in case you have to work with them.
01:22I know a lot of times developers, students ask me what is the most efficient
01:26and for loops are not the most efficient.
01:28Actually, the one that we're going to learn about now is a lot more efficient,
01:32but it's just not a good one because you add complexity to your code
01:36and it makes it harder for you.
01:38And it's very dangerous when you create endless loops and endless loops
01:43is not something you want because you break applications that way.
01:46All right, so let's create it.
01:47It's the wild loop.
01:48Now, the wild loop contrary to the for loop doesn't define its variables
01:52external on its own, so we have to define it externally.
01:56And then I'll create the while, and I'll say while I is smaller than
02:00whatever value we want, let's do the same value.
02:03I'm going to go ahead there whilst I is smaller than that value.
02:07And then we're going to run our create item
02:11and I'm just going to make sure that I'll just send it to the zero position
02:14for now else save it.
02:18If you go back into our application, literally, our rule is right here.
02:21If I run it right now, I'm going to create an endless loop
02:24because my eye is never changing.
02:26So I have to make sure that inside of my eye, my while,
02:29I'm also upgrading my eye, making it grow by one.
02:33Now, if I save it, go back into my application
02:35and we'll see that we should be getting what we should be getting
02:38and that's working perfectly.
02:40But let's continue and let's show you why this becomes really complicated.
02:44So if I continue on with the same line of thought,
02:47but instead of that, I go ahead and I treat our J instead of our eye.
02:52So I'm just going to go ahead here, create a new J variable,
02:55not a new boss, a new J variable, set it to zero,
03:01set that J, test it to our height.
03:06Oopsie, there we go.
03:09Test it to our height and we're almost done really.
03:13Again, what we want to do is we want to also reset.
03:15We want to actually upgrade our J by one.
03:19Now we're going to have a problem.
03:20I'm going to run it anyways.
03:21I'm just going to take this and drop it right in there.
03:24We're not going to create an endless loop.
03:25We're going to create something even not worse.
03:27But what we're going to do is we're not going to get what we want
03:30and I'm going to show you why in a second.
03:32You try to think with me why until I actually tell you.
03:36So if you watch this, you'll see this is not what we hope to get.
03:40And when we're looking at this, we'll see that this is very odd, right?
03:43We're only getting our value and it's only going downwards.
03:47Now the reason is, is because our J in the beginning starts from zero all the way to,
03:52right, our J is growing.
03:53So it grows from zero and it gets all the way to the size of our height.
03:57And that is exactly the first loop.
04:00And we look at it, it's exactly that first loop.
04:03So this is all, that's J when I equal zero.
04:06But we're not getting anything else.
04:08We're not getting I equals one.
04:09Why are we not getting I equals one?
04:12Because when I equals one, what is the value of J?
04:15The value of J is the total item type.
04:18So it's going to always skip this and all the future loops because J is already reset.
04:23Well, J has already passed this amount.
04:26So for us to be able to actually use this, I have to go back to my J
04:30or in the end here right after or in the beginning, J and set it to zero again,
04:37because we need it to actually run again.
04:39So by setting it to zero, I'm basically saying, hey, I'm done with that.
04:43We're going to restart that.
04:44So this loop will happen again.
04:46And if I run it again, we're going to see that we're going to get here that value again.
04:50Now, if you're asking me now, so why would I ever want to use this and not use four loops?
04:55Let me show you an example.
04:57I'm going to, instead of setting my J to be zero, I'm going to go ahead and set my J to be I.
05:03And what's that, that's going to do, it's going to create a triangle in my screen
05:06because each time there's going to be one less J items that are being created.
05:11So if we run this one more time, let's see what will happen.
05:15Bam.
05:16So you see, you create a little bit more sophisticated things, but you could definitely do it.
05:20So it's over four loops.
05:21How about that is for homework.
05:22Go back to the four loops and try to figure out a way to do it with the four loops.
05:26And I said we're only having one more video, but it's one more topic.
05:30I want to continue this in the next lesson.
05:31So I'm going to break it down into a little bit smaller chunks.
05:34We did the while.
05:35I want to show you the do while very similar.
05:37It'll be a short video.
05:39See you in the next lesson.

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!