Hey, what's up guys? My name is The Cherno. Welcome back to my C++ series. Today we're going to be taking a look at loops.
So when I say loops I'm specifically talking about for loops and while loops. To put it into simple terms loops are basically things that we can write in our code when we want to perform certain operations multiple times. So a really simple example is what if we wanted to print hello world five times, right?
Would we just copy and paste that line of code five times or maybe put that code into a function and then call that function five times, like literally five times in our code? Should we do it that way? Or can we just use a loop to say, hey, I want this code here to run five times in a row? Loops are also important kind of for big picture stuff.
So picture a game, right? If you've written a game, odds are you want to keep the game running, right? You don't want to just like run the game and render one frame and exit and that's it, right? Game done, like one frame, I rendered my frame, I'm going to exit, I'm done now.
That would be very weird. You want to keep the game actually playing and so for that what you need is something called a game loop And a game loop is just something that essentially says hey Like while the game is running so while the user hasn't decided to quit the game Keep updating the game keep rendering the game keep moving all the characters around keep doing everything like over and over again Frame by frame so loops are very important programming they're used in like every program right it's the same thing as like conditionals which we talked about last week video up here that kind of stuff right this is much like that like you need to know this stuff really well because you're going to be using it as one of your primary tools when you actually write code when you write a program so first of all let's start with for loops the example that i gave earlier was printing hello world five times and again we could achieve that just by copying and pasting this log call five times and if i run this card You're going to see that we of course do get Hello World printed to the console five times. However, to make our lives a little bit easier, what we can do is write a for loop. For loops start with the keyword for, and then basically break up into three parts. Each of these three parts are separated by a semicolon.
The first part is a variable declaration, so usually you would declare some sort of variable here. So we're going to write int i equals zero. Now the variable name being I is a little bit of a convention. Some say it stands for iterator Which of course is relevant here because we'll be iterating over this code. You can really call this variable absolutely anything It also doesn't have to be an integer and it also doesn't have to be equal to zero After I show you guys a simple for loop We are going to break it down a little bit and see what we can do because for loops are actually really really flexible and you Can really like make them do pretty much anything you want The second part is a condition.
Basically, this says that as long as this condition is true, I'm going to keep executing code inside the for loop. So in this case, since we want this code to execute five times, right, this log, hello world, we want to call that five times, we're going to write i is less than five. And I'll explain how this ties in together in just a second. The last statement is basically code that will be called before the next iteration of the for loop.
So in this case what we want to do is write i++ which basically is the same thing as saying i plus equals one or i equals i plus one which basically means that we're going to increment our i variable which starts at zero of course here we're going to increment it by one we're going to plus one it every single iteration of this for loop and then of course we provide a body which is basically just saying that anything in this body is the contents of our for loop so this is actually what's going to be looped this is the code that will be executed multiple times i say multiple times but of course these conditions are going to determine whether this code is executed at all or maybe once or it could be a hundred times right it's all depending on this condition let's go ahead and stick one of our log calls into here and I'll get rid of all of these extra ones If we hit our 5 to run our code, you'll see we get the same result. 5 times hello world. Okay, cool.
So let's break this down a little bit. This declaration is the first thing that will happen. When our instruction pointer reaches this line of code here, it will first of all declare whatever is written in here, right?
So in this case, we're creating a new variable called i. What it will then do is immediately check to see if this condition is true. If this condition is true, we're going to go ahead and jump to the body of our for loop and execute whatever is there. When we have finished executing the body and we hit this curly bracket here, this closing one, we're going to go back up to here and perform whatever code is written here.
So in this case, we're incrementing i by 1. After that happens, we're going to go back to the condition and check to see if the condition is true. Now of course at this point, i used to be 0, now we've incremented it by 1, so now we're checking if 1 is less than 5. It is of course less than 5, so we're going to jump back here and perform this code. This is going to keep going until finally i will be equal to 4. We're going to run this code.
jump up here, increment i by 1, which gives us 5, and now we're comparing if 5 is less than 5. However, 5 is not less than 5, because 5 is equal to 5, so this condition is false, which means we now jump to line 10 over here to execute cn.get. And that, in essence, is how that for loop will actually run. run five times.
Now, I just want to stress that these three parts of this for loop declaration are exactly what I said they are, right? This is just code that will be executed at the beginning of the for loop once only. This is a comparison or some some kind of boolean that will be evaluated before the next iteration of the for loop. And then this is some kind of dec-like it's some kind of code that will be executed at the end of our for loop.
So what I could do is like Literally, I could say actually in I equals zero, I'm going to put that over here and leave this completely blank. Just like that, right? That's perfectly valid code.
I could also say that this I plus plus I'm actually just going to drop it here, right? Because it's that's going to have the same result and if I run this even though it looks a little bit weird We're actually going to get the exact same result because we haven't actually changed the behavior of this code at all We've just moved some things around similarly I could write a boolean here called like condition or something set it equal to true and have that to be my actual I Condition, right? So I've got my condition, I'm gonna do i++ and then I might do something like if i is not Less than 5 anymore, right? I'm going to set condition equal to false and if I run this code You can see that I'm still gonna get hello world printing five times because again, I haven't changed the behavior of my program I've just rewritten the code in a slightly different way But I'm telling you all this so that you know that for loops they can do anything, right? Like you don't have to always be like for in I equals zero eyes less than five I plus plus that's boring, right?
They can contain any kind of statements you want you could also of course call functions in that like for loop save Anything right the possibilities are literally endless You can also get rid of the condition entirely which basically means that it's the same thing as writing true, right? This will never be false. And if I run this it's actually going to be an endless loop, right?
It's never going to end So if I hit f5 right now to run this you'll see that we're going to actually keep printing. Hello world as our program keeps running until we manually terminate it. So that's pretty much it for for loops.
I mean they're really really simple for doing like for running code multiple times. So for example I want to print hello world five times. Fantastic for that, really easy to set up for that.
Also they're really useful with arrays when you want to kind of go through an array kind of linearly like that as well. Very useful for that. The next thing that we'll move on to is while loops. So a while loop is much like a for loop it's just that it doesn't have this kind of statement at the beginning and this statement at the end. It's just got a condition.
So basically to write a while loop, we'll write the keyword while and then inside here we put some kind of condition. So for example, i is less than five, right? As an example. And of course i was declared up here.
So if we run this, this will basically perform whatever code is in here as long as i is less than five. So if we were to rewrite our for loop, let me just bring back the for loop to what it was originally. If we wanted to basically replicate this for loop in here, we would have to write int i equals zero out here, and then do the i plus plus at the end of the loop.
So over here, right? And then right now what we have here is actually exactly the same. I'm just going to, in between these, I'm just going to print like a whole bunch of equal signs just to separate the two statements. But if I run this, you can see that we get the same result from both loops, right?
They're both printing hello world five times. So why use a while loop instead of a for loop? Well, Like when would you use a for loop instead of a while loop?
Basically, it comes down to whether or not you need a variable right because these loops are identical you can use them interchangeably, right? It's more a matter of convention or a matter of style than it is an actual card rule because again Like there's no actual difference between the two loops They can do exactly the same thing if you so want want them to but the conventions are basically if you have a certain condition that already exists that you want to just kind of compare. So for example I mentioned that whole game loop example where you had a variable called running and you might want to kind of keep that game loop going as long as that running variable is true because running equals true basically means that your program is still running. If you wanted to do something like that I would probably use a while loop because all we need is a condition.
We don't need to be changing the condition after every iteration. We don't need to declare the condition to start off the loop. It's already a variable that we've got or a function call that we can use and that's just going to be completely like... fine, right?
It's not something that we actually have to keep kind of updating or set up initially, none of that stuff, right? Whereas if I wanted to go through an array and the array had a certain size, for example, my array was 10 elements long, then I would use a for loop because first of all, I want to run that code exactly 10 times. So I want to keep track of some kind of variable and only run that loop 10 times.
But also that variable will be useful for accessing elements inside that array because if I want to access every element in a 10 element array, I need some kind of offset so that I can like an index so that I can access an element in that array And of course we're going to talk about arrays in the future But that I variable that we happen to be tracking and it's going to go 0 1 2 3 4 5 as our iterations go on It's going to be absolutely perfect Perfect for that. So we've got for and we've got while but there is actually one more loop that we have and it's called a do while loop. Now these aren't that useful.
I don't personally use these that often but they are there and they do have some uses although again you won't be seeing these anywhere near as much as for loops or while loops. So basically we write the do keyword and then we have a body and then at the end we write while and we have some kind of condition. So for example, again, I'll just do i is less than five. The only difference between a do while loop and a while loop is that this body will be executed at least once no matter what, right? So for example, if we change this into i situation, so that instead we actually just have a condition and it might be equal to false, if I stick that condition in here, this while is actually going to function much like an if statement in a sense that if this is false, well then it's never going to run the code in here.
However, a do while loop, even if this is false, right, if it is set to condition, condition is false, it will function as if it's not a loop. It's going to run the code here once, it's going to come down to the while, look at the condition, oh, the condition's false, so I'm not going to loop. And that's pretty much it, that's how do while loops work. Alright, so that's pretty much it for today's video, hope you guys enjoyed, very basic introduction to loops.
We're going to be using these a lot throughout the series, they're used in like pretty much every algorithm you can think of. Loops are extremely useful when we talk about arrays. We'll be talking about how we can use for loops to access arrays and all that stuff so everything will tie in nicely.
I'm going to do a more in-depth look at them sometime in the future where we're actually going to look at the disassembly and look at the actual CPU instructions that get generated for loops and take a bit of a deeper look. I just didn't want to make this video too complicated so the link to that in-depth video will be somewhere in the description when that video actually comes out. I hope you guys enjoyed this video, if you did please hit that like button, you can follow me on Twitter and Instagram and if you really like this series and you want to see more episodes and you want to support the series You can support me on patreon.com forward slash the churner and I will see you next time. Goodbye