C++ Programming Tutorials - 11 - Do While Loop - Eric Liang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to my 11:30 plus programming tutorial my name is Eric and in today's episode we're finally going to learn about the do-while loop in C++ now in the previous tutorial we last left off on creating a minimum voting age requirement checking program using a while loop and this is how it went so we were to quickly compile this program and run it it would show us the message please enter your age and then it would keep on asking that until we give it a value of 18 or older so if we were to type in a few values 3 let's say 6 and let's say 10 let's say finally 24 which is greater than 18 it will change and as you can see it shows congratulations you are eligible to vote so one of the other topics that we left off of was hey I noticed that line 10 and 11 is the same as line 15 and 16 how come we have to be so repetitive can't we just remove these two lines and then simply compile them and have it do the same thing except two lines less well no because there would be build errors and that is caused by the user age is not initialized at this point because we need a user age to be initialized in order to do the comparison in the first place inside the while loop in order for us to even run these lines of code so in order to fix this where we use two lines less pieces of repetitive code we would have to first learn about the do-while loop so if I were to push everything down here for a bit for a second let's learn about the do-while loop syntax it goes like this do followed by curly brackets and then while and then the parentheses and finally a semicolon and inside the do brackets this is where you type in two statements that will repeat as long as the conditions inside the parentheses is true so statements that you want to repeat goes in here and then inside the parentheses as usual we put our conditional statement so in our case we would want to convert this entire program so it's using a do-while loop instead of a while loop how you might do that let's create a new project so new project and then let's say do-while loop you can name it whatever you want in the next empty project finish and then as we know inside the source files we create a new item make sure it's a CPP file give it whatever name you want I'll call mine main dot CPP which is pretty standard so you know the drill include iostream using namespace STD into your main followed by return 0 whoops return 0 awesome so let's create our variables that will start at age values go into user age integer minimum voting age requirement equals 18 again if your country is different from 18 for the voting age requirement then feel free to change the number 18 to whatever is appropriate for you and then let's create our do-while loop do this while this is true okay so again we want to ask the user for their age and we want to ask that at least one time no matter what so in this case let's see out that instruction please enter your age and then see in user age and then we would want the program to keep on asking the user these two lines of code until their age meets the minimum voting age requirement or is greater than the minimum voting age requirement so to do that we use user age less than minimum voting age requirement and y less than same reason is last time we want to run this as long as the user age does not meet the requirement which is in this case less than the value of 18 so once the users age meets the minimum voting age requirement we would run this line of code which is outside of the do-while loop same idea as before with the while loop except we're using a do-while loop design so see out congratulations you are eligible to vote please proceed to the voting booth so if we were to quickly run this program it should give us a similar experience so please enter your age let's say 5 as you can see we are greeted with please into your age again so 10 and then finally let's say 24 which is greater than 18 and there you go it shows congratulations you're eligible to vote please proceed to the voting booth so that works and let's say we want to do this again except this time we'll type in the value correctly so let's say 18 BAM it shows congratulations you are eligible to vote please proceed to the voting booth and as you can see this time we only have these two lines of code and we don't have to type this outside of our loop and that's because the great thing about the do-while loop that makes it unique is that the do-while loop guarantees that these statements inside the do portion to run at least one time no matter what so if you are confused about when to use the do-while loop versus a for loop versus a while loop well that's your answer if you want your loop to run the statements inside of it at least one time no matter what then you use a do-while loop but of course make sure the values inside does not rely on things that need to be initialized beforehand but in this case our situation is okay because we're going to be initializing it inside the loop which is fine because the condition runs after these statements get run by the program and that concludes our tutorial for today's do-while loop programming tutorial video thank you for watching and please hit that like button if you liked it and subscribe - stay tuned for more because the more interesting stuff will programming is right around the corner in the next tutorial we will be learning about the for loop again thank you for watching and have a great day you know a lie [Music] [Music] [Music]
Info
Channel: Eric Liang
Views: 710
Rating: 5 out of 5
Keywords: C++, CPP, C++ programming tutorial, how to code, how to program, C++ tutorial, how to program in C++, C++ programming for beginners, learn how to program, C++ tutorial for beginners, C++ do while loop, learn coding, code, visual studio, loops, C++ Loops, do while loop
Id: fWgYimjutY4
Channel Id: undefined
Length: 7min 23sec (443 seconds)
Published: Thu Jun 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.