Java Programming Tutorial 25 - Do While Loop

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yo what's up my boys and girls today we're gonna be talking about the do-while loop now this is very similar to the while loop the only thing is it's more what's the word come compulsive its kind of mean when I go to the store I'll go in there I'll buy something and then try to figure out if it was a good idea after the fact all right so the do-while loop is going to do something and then then it'll have the condition on whether or not it should continue to do it you know so I goes in there buys $350 pair of headphones I tried doing that this week and my wife was like nope and then it's like you know let me do my budget oh I should probably stop it's such a terrible explanation but I'm gonna go with it you know what you shouldn't stop though you definitely shouldn't stop preparing for technical interviews technical interviews are the most feared thing in technology and it doesn't have to be the case prepare yourself by going to my sponsor pram when you sign up for pram you know what happens you get paired with another person who's going to criticize your interviewing skills sounds bad but it's actually a good thing because then you can improve and then when you got an interview with Google and Amazon and Twitter and Facebook and everyone else you can be like yo man I know what I'm doing cuz I prepared with pram go check them out I started using pram for data structures and algorithms but they have all kinds of other topics all right now let's move on to the do-while loop you can see in the code we have from the previous video we have a lot of repeating stuff we have this guest the password twice we scan a next line twice here and here like why do we have all this extra code shouldn't we be able to convince this a little bit differently well here's what I'm gonna do I'm going to make a do-while loop and it's just going to ask the person for the password at least once and then if the person's right it'll keep doing it so the difference between a do-while loop and a while loop is that the do-while loop always executes at least once while loop doesn't have to for example if I run this code here and I get the password correct let me in it just jumps over the Y loop all together so here's what we're gonna do we're gonna change this to a do-while loop and here's the structure we're going to have do and then we're going to have curly braces and then after the curly braces we're going to have while I'm gonna structure like this and the condition goes here so it's the same thing we still have the initialization the condition and the update just the structure is a little bit different so we're gonna cut this line here and put that in the do-while loop because we're going to execute that at least once because we always want to ask the person for a password will define the password up before the loop just so it's always there for us oh and another thing after the while loop you're going to one a semicolon it's really easy to forget because when you do a normal while loop you don't put a semicolon here but when you do it do while loop you need to put them at the end here we're also gonna take the scanner we're gonna move that to Oh cough oh gosh you scared me my dog's freaking out now we're gonna move that to the top as well parrot and then we're gonna move this guess to inside of the do because you know we're going to get a guess at least one time but we're going to define it outside of the while loop so we're gonna say string guess and then we'll just assign a value to it inside of the do-while loop and the reason we're going to do that is so we can use it inside of the while here because if we define the variable inside of these curly braces it's only going to exist inside of these curly braces definitely not what we want now this expression is gonna stay the same so I'm just gonna cut that paste that right here and now these two lines we no longer need those so we can just get rid of this while loop all together all right now let's run it make sure it works guess the password let's get it wrong it keeps asking us I can't spell and then once we finally get it right it works if we defined guests in here like this well then inside of this while section it's gonna say that this is undefined it's not able to resolve this identifier meaning it doesn't exist so you definitely need to define anything that you're gonna use inside of the wild piece outside of the do section and then you don't redefine in here you just leave it as guess because when you say this well that's not gonna work because we already have a guess in scope here so it's saying dude you have a duplicate variable we're not gonna let you run this thing some other programming languages would let you do this it would just mask this variable but it's not gonna do that in Java you can't create a variable and math a variable that already exists so no redefining variables guys keep it like that so to go through this from beginning to end we first set up the basics what we're going to be using throughout this loop which is the definition of what the password actually is we create a scanner object then we declare a guest variable which we're going to assign to later inside of the do section we ask the person for what we want we get that value and while that value is not equal to the password we keep doing the do section and then once you're done it'll go down here and start printing this stuff down here and then it'll close the scanner hopefully that's crystal clear for you guys make sure you're shopping does not look like this because if you do this what's gonna happen is you're gonna buy too much and your budgets not gonna be okay all right that's all I got for you guys hopefully this video is helpful please consider subscribing and check out the links in the description for some cool things and stuff so check out alright peace out [Music]
Info
Channel: Caleb Curry
Views: 29,719
Rating: 4.965909 out of 5
Keywords: java programming, java, programming, tutorial 25, tutorial, do while, loop, while, while loop, do while loop, java tutorial, java tutorial for beginners, beginners, caleb, curry, calebthevideomaker2
Id: A7yvQrDe4ks
Channel Id: undefined
Length: 5min 42sec (342 seconds)
Published: Wed Nov 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.