do...While Loop

2 minutes
Share the link to this page
Copied
  Completed
In most computer programming languages, a do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.

Transcript

Hello in this video I am going to cover the do while loop. If you haven't watched the Wally video I highly recommend that you go and check right okay, so it's a while loop it runs 10 times and it's pretty simple okay. So what happens if I was to set this value to 15 so as you can see 15 is not less than 10 So what should happen is nothing the loop should never be ruined because the condition is never met. So let me show you the format of a do while loop what you do is you actually put this void condition at the end after the curly brace Yeah. Then to start you put dude that is it. So that's all I've done.

Let me run that and if the weapon so we get Luke 15 but then beaten him on anymore weapon for change it back to 00 to nine. So you see that the only difference is is when we have a value that prevents this condition from being true from the start. And that's what happens. The do while loop will get run at least once. Because the actual check is, you know is done at the end of the loop you're running one doesn't matter what the variable law for your do the code in this case it says loop than the number 15. Yeah, add one to this.

And as this is still let them greater than 10. That means the actual condition is false. Anyone wrote more, but that's the only difference between the wall loop and the do while loop is if you know that you have a you know a loop, they have some code in it and you need to execute it at least once. Then you will use a do while loop. Okay, so where might you might use this? Imagine if you are getting some input from the user.

Maybe it's a it could be a registration form and they are inputting it. username. And you need to get at least once, obviously. So if you get that input once, and it all passes all the checks can do a bunch of if statements and you know, do various checks, yes, that it doesn't matter what the checks are, do the checks, if you don't pass it, then that's fine. You can basically say, you know, in the wall loop is just his username, you know, failed, essentially. So how the verification code, if we had about done, it's fine, we don't need to run the loop any more times.

If he has failed, what you can essentially do is get input from the user again, and then do the checks and just keep doing that till the condition is no longer true of it, basically, failing the verification. So that's where you might want to use a do while loop. So that's it. If you have any questions, feel free to drop me a message. And as usual, I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.