Loops in C++ (for loops, while loops)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 loot so as have loops I'm specifically talking about for loops and while loops to put it into simple terms moves are basically things that we can write in our code when we want to deform certain operations multiple times so a really simple example is what if we wanted to print color 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 rented 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 it's a very important for programming they 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 fold it the example that I gave earlier was printing color world five times and again we could achieve that just by copying and pasting this log call five times and if I run this code you're going to see that we of course do get hello world printing to the console five times however to make our lives a little bit easier what we can do is write a for loop for loop to 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 are going to write int I equals 0 now the variable name being I is a little bit of convention some say it says the iterator which of course is relevant can because we'll be iterating over this code you can really call this variable absolutely anything at all so 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 the down a little bit and see what we can do because bullets 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 write this log color world we want to call that five times we're going to write AI 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 AI plus plus 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 variable which starts at zero of course here we're going to increment it by one we're going to +1 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 forward so this is actually what's going to be looped this is the closure 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 one if we hit our 5 to run our code you'll see we get the same result five times hello world ok cool 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 from 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 able to hear 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 were incrementing by one 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 zero now we've been chromatid by one so now we're checking if one is less than five it is of course less than five so we're going to jump back here and perform this code it's going to keep going until finally I will be equal to four we're going to run this code jump up here increment I by one which gives us five and now looking pairing if five is less than five however five is not less than five because five is equal to five so this condition is false which means we now jump to line ten over here to execute end gap and that in essence is how that folders will actually run five times now I just want to stress that these three parts of this flawless 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 upon some kind of bullying that will be evaluated before the next iteration of the for loop and then this is some kind of death looks like 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 Iran we'll 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 code at all we've just moved some things around similarly I could write a bully in here called like condition or something set it equal to true and have that to be my actual condition right so got my condition I'm going to do I plus plus and then I might do something like if I is not less than five anymore right I'm going to set condition equal to false and if I run this code you can see that I'm still going to get hello world printed in 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 fourteen i20 eyes worse than five bucks plus that's boring right they can contain any kind of sentence want you can also of course call functions in that like for loop setting 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 that's pretty much it for for loops I mean they're really really simple for doing like running code multiple times 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 and array kind of linearly like that as well very useful for that the next thing that we'll move onto is wild it 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 payment at the end is just got a condition so basically tried a while loop will write the keyword while and then inside here we put some kind of condition so for example I is less than five right has an example and of course I would 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 to follows to what it was originally if we if we wanted to basically replicate this for loop in here we would have to write into I equals 0 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 be two statements but if I run this you can see that we get the same results from both loops right there both printing hello world five times so why use a while loop instead of a for loop or 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 can do that already exists that you want to just kind of compare so for example I mentioned that whole game would 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 with ten elements long then I will use as follows because first of all I want to run that code exactly ten times so I want to keep track of some kind of variable and only run that loop ten times but also that variable will be useful for accessing elements inside that array because if I want to access every element in a ten 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 is going to be absolutely perfect for that so we've got 4 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 follows or while loops so basically we rise to 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 eyes less than 5 the only difference between the 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 if we change this into I situation so that instead we actually just have a condition and it might be equal to solve 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 this is false right if it is such a condition conditioners fault it will function as if it's not a loop it's going to run the code here one it's going to come down to the wire look at the condition are the conditions 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 enjoy 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 our arrays we'll be talking about how we can use foliage 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 enjoy 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 back home for slash the show now and I will see you next time goodbye [Music] [Music]
Info
Channel: The Cherno
Views: 230,866
Rating: 4.9527836 out of 5
Keywords: thecherno, the, cherno, project, thechernoproject, c++, how c++ works, learn c++, c++ tutorial, game, programming, development, engine, game programming, game development, how to make a game, tutorial, source, code, complete, game engine, how to make a game engine, opengl, glfw, C (Programming Language), loops
Id: _1AwR-un4Hk
Channel Id: undefined
Length: 12min 20sec (740 seconds)
Published: Sun May 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.