Buckys C++ Programming Tutorials - 18 - while Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome to your 18th C++ tutorial and in this is rural I'm going to talk to you guys about a while loop and every saying alright Bucky take a step back I don't even know what a loop is and now you're trying to talk to me about special kind of loop alright let me step back a loop is basically in computer programming if you want to take a certain bit of code and run it over and over and over again you can go ahead and you can just type it on like a hundred different lines and no keep running or you can throw it in something called a loop and what a loop does it says alright write this code only one time and I'm going to run it as many times as you want me to so for example instead of say we wanted to print hello on the screen 100 times instead of writing C out hello on a hundred different lines we can only write it on one line and loop it a hundred times so it's a much easier way to make pieces of code around multiple times so let me show you guys an example and by the way now I can talk to you guys about a while loop there are many different types of loops a while loop is pretty much the most basic type of loop in my opinion it's the easiest so that's why I'm showing this to you guys first so let's go ahead and I'll show you guys an example go ahead and make a variable called bacon and set it equal to zero this syntax for a while loop is this we're going to say while and then in the parentheses right after we're going to give it a test just like the if statement we're going to test this bacon is less than or equal to five go ahead and run this bit of code over and over again so let's go ahead and just run C out armed bacon is and then now say the value of bacon and then end line so now if you were go ahead and run this you would say alright run it bacon to zero bacon to zero bacon to zero and you can see that it keeps printing it out over and over by now it probably printed it out like 20,000 times so you're saying all right it's kind of useful I guess but would it be more useful if this loop would end for example if we only wanted to run it 20 times how would we go about doing that well sometimes people do want loops that never end but typically they don't typically you want to only have this code run like five times or ten times or 15 times so how do we control exactly how many times it runs well in order to do that we usually add a line of code like this again what this test is going to do is test is bacon less than or equal to 5 and since the value is 0 right now this test is always going to be true so in order to make this test false in this loop to stop running we need to change the value of bacon at some point so it is equal to 5 or greater than 5 and in order to do that go ahead and make bacon equals bacon plus 1 now each time we run gloop is going to go ahead and do its thing right here and then when it's done it's going to add 1 to the value of bacon so the first time it runs it's going to be 0 and then it's going to add 1 to it so then it's going to be 1 then it's going to be 2 3 4 5 and by the time it gets to 5 it's going to change it to 6 and not run so let me go ahead and show you guys making 0 1 2 3 4 5 so we already know why bacon rain in this case when bacon was equal to 0 it was less than or equal to 5 1 yup - yup 3 4 5 is 5 less than or equal to 5 yep so it printed that out but then whenever it said all right the value of bacon is 5 so I'm going to print out bacon is 5 and then I'm going to add 1 to bacon so now bacon equals 6 all right let's try this loop again is 6 less than or equal to 5 no finally the test is false so I can quit running my dumb loop so that is how you add um again like I said and when okay take a take a breath Bucky you're jumping too fast sometimes people want loops to run forever and when they do you do something like the first example I told you but typically and I want to say like 95% of the time people build loops that they only want to run a certain amount of times for example five times if you only wanted to run at five times or actually it was six in this case you would add something called this line right here and this pretty much just changes the value of bacon so at some point that test has to be false if you wanted to run it 20 times you would go ahead and just change that to 20 and check it out bacon is 1 2 3 4 5 all the way to 20 pretty cool huh so you can change how often your loop runs by a couple things it's starting point it's ending point or how much you increment that value by again you can add 3 to each time if you want and then you get something like this one at 3 2 4 7 10 13 16 19 and when you added 3 to that and it was 22 well 22 is indeed not less than or equal to 20 so that's when it stopped the loop so that is the basics of a while loop again all the while loop does is take a certain bit of code and runs it over and over and over again until the test is false and again like I said at the very beginning of this tutorial there are many different loops that allow you to take a bit of code and run it over and over and over again a while loop is pretty much just the most basic kind so make sure whenever building your while loops not to forget to change that value or loops going to run forever and who wants that so for now thank you guys for watching I'm actually going to go make some bacon right now so you guys sit tight re-watch this story and once you're good once you understand the syntax of how a while loop works you're ready to move on to my next video so again thank you don't forget to subscribe and I'll see in the next video
Info
Channel: thenewboston
Views: 552,451
Rating: 4.9584489 out of 5
Keywords: tutorial, beginners, game, class, program, pointers, lesson, thenewboston, bucky, roberts, computer, string, vector, download
Id: KLKhsaOPnLk
Channel Id: undefined
Length: 6min 9sec (369 seconds)
Published: Wed Apr 06 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.