Loops in C++ (for loops, while loops)
The Cherno
·
Intermediate
·9y ago
Key Takeaways
The video demonstrates the use of for loops and while loops in C++, covering their syntax, usage, and differences. It provides examples and steps to write and use these loops in programming.
Full Transcript
hey what's up guys my name is the chano 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 Hol 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 funun 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 rended 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 for 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 code you're going to see that we of course do get Hol 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 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 in I equals 0 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 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 full loop so in this case since we want this code to execute five times right this log color world we want to call that five times we're going to write I is less than 5 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 so in this case what we want to do is write I ++ which basically is the same thing as saying i+ = 1 or I = I + 1 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 Loops so this is actually what's going to be looped this is the code that will be executed multiple times I say multiple s but of course these conditions are going to determine whether this code is executed at all or maybe once or it could be 100 times right it's all depending on this condition so 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 five to run our code you'll see we get the same result 5times 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 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 you to be zero 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 five and now we're comparing if five is less than five however five is not less than five because 5 is equal to 5 so this condition is false which means we now jump to line 10 over here to execute cn. getet and that in essence is how that for Loop will will actually 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 full loop once only this is a comparison or some some kind of bullying that will be evaluated before the next iteration of the for Loop and then this is some kind of De 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 equal Z 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++ I'm actually just going to drop it here right cuz 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 bully in here called like condition or something set it equal to true and have that to be my actual condition right so I've got my condition I'm going to do I ++ 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 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 4 in IAL 0 I is less than 5 I ++ 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 statement you anything right the possibility 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 simp simple for doing like for for running code multiple times so for example I want to print hell 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 um the next thing that we'll move on to is while Loops so a while loop is much like a full loop it's just that it doesn't have this kind of statement at the beginning and the 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 5 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 four Loop let me just bring back the for Loop to what it was originally if we if we wanted to basically replicate this for Loop in here we would have to write in IAL 0 out here and then do the I ++ at the end of the 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 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 then 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 uh if 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 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 full loop because first of 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 is going to be be absolutely perfect for that so we've got four 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 alth 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 y Loop is that this body will be executed at least once no matter what right so for example if we if we change this in 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 the 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 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 wire 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 all right 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 of 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 but 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 theecho and I will see you next time [Music] [Music] goodbye
Original Description
Twitter ► https://twitter.com/thecherno
Instagram ► https://instagram.com/thecherno
Patreon ► https://patreon.com/thecherno
Series Playlist ► https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb
BEST laptop for programming! ► http://geni.us/pakTES
My FAVOURITE keyboard for programming! ► http://geni.us/zNhB
FAVOURITE monitors for programming! ► http://geni.us/Ig6KBq
MAIN Camera ► http://geni.us/CYUQ
MAIN Lens ► http://geni.us/ZM3CmG
Microphone ► http://geni.us/wqO6g7K
Slack ► https://slack.thecherno.com
Stream ► http://www.twitch.tv/thecherno
Website ► http://www.thecherno.com
Facebook ► http://www.facebook.com/thecherno
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from The Cherno · The Cherno · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
3D Game Programming - Episode 1 - Window
The Cherno
3D Game Programming - Episode 2 - Game Loop
The Cherno
3D Game Programming - Episode 3 - Arrays
The Cherno
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
3D Game Programming - Episode 7 - FPS Counter
The Cherno
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
3D Game Programming - Episode 11 - Rotation
The Cherno
3D Game Programming - Episode 12 - User Input
The Cherno
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
3D Game Programming - Episode 15 - Textures + More!
The Cherno
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
Episode 18.5 - Creating an EXE File in Java
The Cherno
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
3D Game Programming - Episode 29 - Launching The Game
The Cherno
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
3D Game Programming - Episode 31 - Sprites!
The Cherno
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
The Cherno
Genesis - My Game for Ludum Dare 24
The Cherno
Vlog + Ludum Dare Results
The Cherno
Game Programming - Episode 1 - Resolution
The Cherno
Game Programming - Episode 2 - Threads
The Cherno
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
The Cherno
Episode 5 - Buffer Strategy
The Cherno
Game Programming - Episode 6 - Graphics Initialized
The Cherno
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
Game Programming - Episode 8 - The Screen Class
The Cherno
Game Programming - Episode 9 - Rendering Pixels
The Cherno
Game Programming - Episode 10 - Clearing the Screen
The Cherno
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
Game Programming - Episode 12 - Negative Bounds
The Cherno
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
The Cherno
Episode 15 - Tiles
The Cherno
Game Programming - Episode 16 - The Map
The Cherno
The Walls 2 - Minecraft PvP Survival Map
The Cherno
Game Programming - Episode 17 - Key Input
The Cherno
Game Programming - Episode 18 - Controlling The Map
The Cherno
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI