Java Fundamentals Do While Loops
Transcript from the "Do While Loops" Lesson
>> So let's look at another loop. This time we're going to add numbers. So we're gonna write a program that allows a user to enter two numbers and then sums up those two numbers. The user should be able to repeat this action until they indicate that they're done.
[00:00:17] All right, so we're going to make a new class under, I'm gonna close these other packages, under the loops package, and we'll call this one AddNumbers. We'll create our main method, and we want, let's go ahead and open our scanner right away. Okay, I need a variable to control the condition.
[00:01:03] So I'm gonna set it to Boolean, cuz remember, the conditions have to result to true or false. So I'll call this one again. And I'm not gonna set it, I don't know if they wanna run again or not, right? I know that they wanna run at least one time.
[00:01:17] The program runs and then at the end, I'll ask them, do you wanna do it again, right? This could be useful for things like games, they play one time, and then you say you wanna play again, and they can do that. So with this loop where we want something to run at least once, and then run again based on a condition, this is called the do-while loop.
[00:01:39] So you start this loop with the word do, and you add a set of curly braces, you add any code that you want executed inside of that, and then at the end of the do, you write wow. And then you put the condition at the end, okay? So we can say wow, again, wow they say they wanna run again, right?
[00:02:02] Now, of course, this is complaining because we haven't initialized it. We can initialize that in the do, all right? So inside of the do is where we'll add all of our code that says, enter the first number. So enter the first number. And we'll read this into a variable.
[00:02:25] We'll call this num1 = scanner dot NextDouble, okay? And we'll ask them for the next number. Enter the second, oops. Enter the second number, NextDouble. All right, perfect. And then we can sum this up, print that out. So let's just print it out. We can do our calculation right here, and this is interesting.
[00:03:13] Okay, inside of here I wanna say, num1 + num2, all right? The reason I put this inside of parentheses is because I am utilizing this plus sign in two different ways, basically overloading its purpose. So I can use it here to append some value to a string, and then I'm using it here for addition.
[00:03:39] So I don't want them to think if I didn't put this it'll probably append the first number and then the second number. But by me putting in parentheses, I think it's gonna add them both together, we shall see, all right? Now remember, I gotta get a value for, again, so let's go ahead and ask them if they want to do it again.
[00:04:00] Would you like to run again? And then we'll store that inside of this, again, variable. So we'll say scanner.nextBoolean(), yes. Okay, And there is our do-while, yeah.
>> What would be a valid response for a Boolean?
>> [CROSSTALK] Very good question. Okay, let's see. So the short answer is true or false.
[00:04:48] But people, if you asked it in this way, I wouldn't think to put true, I may put yes. So something like that, right? One or zero are also valid Boolean. So one would be true, zero would be false. So do you want to say, enter one for yes or zero for no or something like that?
[00:05:09] We could do something like that. Would you like to 0 for no, 1 for yes? Let's make sure that works. All right, we got a couple of things to make sure that works. [LAUGH] Let's run it. All right, first number we'll say 5, and second number is 4, the sum is 9.
[00:05:36] Great, so we made sure that this was actually adding those two numbers together. That was great. Would you like to run again? 0 for no,1 for yes. Let's say 1 for yes and make sure that it recognizes that. It does not, okay. So input mismatch, I think all I can say is true or false.
[00:06:00] False, renew. Now what you would do in real life is not give people these horrible options. You would give them something else that was more user friendly.
>> We wouldn't be working in the console in the first place.
>> We wouldn't be working in the console. We would have a nice pretty button, right?
[00:06:19] Okay, so let's see 8 and 1. Also 9 in true. True, all right, [LAUGH] so true works, okay? And so the numbers 7, and 3, 10, and then false shit ended, all right.
>> What if we wanted to, if they wanna keep going, they wanna use the previous values that they've already given?
>> Good question, okay. So they would not be able to do that, because when this executes again, everything, notice here that we are declaring num1. So this declaration will start over again when this is run. If we wanted to keep the values, and I think we do that in the next program, we could do something like that.
[00:07:14] But we would have to define these variables outside of the loop. Because when we do it inside of the loop, it's basically starting over each time.
>> So you would need some kind of variable running total or whatever?
>> Exactly, exactly, okay? Now, I'll say this about the do-while loop, it's not very heavily used in the industry.
[00:07:37] Within my 20 plus years of coding, I have probably used the do-while loop a handful of times. People typically prefer the while loop. It's a matter of preference. I've seen some developer shops where they will straight up like reject your code review because you used to do-while and say, turn it into a while loop.
[00:08:04] The difference With the do-while loops is that the condition is checked after, meaning you are running this at least one time. So we would have to initialize this variable to say true or something, and then turn it into a while, right? I think it's a bit silly, but again, that's what people are doing, all right?
[00:08:25] So some things to remember about do-while loops. They're also condition controlled. They test the condition after the loop. And it's best to use this if that loop should run at least one time, and then conditionally more times after that.