Check out a free preview of the full Java Fundamentals course:
The "While Loops" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie walks through using a while loop to create a program with input validation that runs until the defined condition is met. Adding extra input restrictions using the logical operator || OR is also covered in this segment.

Get Unlimited Access Now

Transcript from the "While Loops" Lesson

[00:00:00]
>> So that is it for decision structures, the next section is on repetition structures. These are also known as loops. They're a way to repeat a block of code as many times as necessary. Okay, so there's a few different types of loops and we'll discuss how to use each of them.

[00:00:25] So, the first program we'll write is input validation, and we'll use a loop here. This goes back to your question that you asked earlier, what if we wanted to kinda keep doing that? So, in this program, it says each store employee makes $15 an hour. Write a program that allows the employee to enter the number of hours worked for the week.

[00:00:47] Do not allow overtime, okay? So let's go ahead back to our IDE. Since we're on a new chapter, we'll make a new package as well. So under project. Under Java, main Java, I'll make a new package and I'll call this one loops and inside of here we'll make a new class.

[00:01:23] We can call this one gross pay input validation. All right, so I'll make my main method and let's go ahead and initialize some variables so we know that the rate is $15 and the max number of hours is 40 cuz we're not allowing any overtime, right? And then we can ask them, how many hours did you work, okay?

[00:02:04] And we'll scan that result in. Import the scanner, and we'll set that to a double, if I do that as double, sorry I'll make these double too. Okay, double hours worked. Notice that CamelCase= scanner.nextDouble, okay. And now we want to validate this input. Now, if we think about this, let's say that the person enters 45, that's invalid.

[00:02:56] We can say invalid in the program can just end. They would have to start all the way over from the top or we can say, okay, give me your input, let me check if it's valid. If it's not valid, I wanna prompt you again, right? And in order to prompt this person multiple times, the best way to do that is using a loop because a loop repeats statements, okay?

[00:03:20] So the loop we'll use for this is called a while loop. And we started off with the word while. We have a set of parentheses and inside of the parentheses we're going to add a condition for running this loop. So whatever this condition is, is saying, while this condition is true, keep executing the statements that I'm adding inside of here.

[00:03:45] So in our case, we wanna keep executing while the hours worked is greater than the max hours, right? Cuz that's a faulty condition. So inside of here we'll print out and let them know invalid entry your. Hours must be between 1 and 40, for example. Okay, now that we've let them know that, we want them to be able to input a new entry, okay?

[00:04:29] So you might wanna say try again or something so that they know, try again. They know to enter something new. And we're going to store that in this hours worked variable again. So we'll say scanner.nextDouble. Now this is really important, I want you to look at this while statement.

[00:04:55] The condition for the while is based on two variables, hoursWorked and maxHours. If we maybe made a new variable here, let's say double newHours. Anyone can tell me what's wrong with this? Yeah.
>> The conditional statement would still be evaluating on hours worked so we wouldn't have changed it.

[00:05:20]
>> Right, so we would not have changed the two values that are being evaluated therefore, if we ever got inside of this loop and these values never changed, the condition is always gonna be true. You have an infinite loop, so whatever condition you are checking for in the while, you have to do something inside of this loop that affects that condition in some way or has the possibility to affect the condition in some way.

[00:05:49] Now when I say hour's work, they could very well into 50 again, that's okay. It still changed, right? There's a possibility that they enter something valid. All right, okay so that's it for the loop. We just basically tell them in valid however many times that they keep writing invalid input.

[00:06:11] And then once that is valid, let's say, they say 40, is 40 greater than the max hours 40, that will become false and we will finally get outside of the loop, okay. You can add any other code you want here, so I'll go ahead and close our scanner and then also calculate their salary and print that out.

[00:06:38] So we'll say double grossPay = hoursWorked times we had a rate rate yes and then we'll print this out. Okay, should we run it? All right? So how many hours did I work? Let's say less than 40, so we'll say 38. Great, that worked just fine. Now, let's say 42.

[00:07:25] All right, now when we say 42 it'll say invalid entry. Now we're inside of this loop here. Try again, I'll say 45 this time, right? Hours work will become 45, it's 45 greater than max hours, true. So they'll ask us again, we're still inside of that loop, right?

[00:07:47] 50, still inside of the loop, if I then say 40, which is not over, I should get outside of the loop, so it'll say it's 40. Greater than 40 that should be false and now we're outside of the loop. Questions on while loops.
>> Here's the question on while loop, but on the scanner next don't worry if you don't enter in a double or an int., what does it do?

[00:08:19]
>> It'll crash, the program will crash because it'll try to set some sort of data type into this variable that was declared as a double and it'll all fall apart.
>> Okay, so you'll just get a system.
>> We'll show you. I'd like you to see for yourself.

[00:08:35] Let's say, hello and the program crashed, right? And it said, input mismatch exception.
>> So we said anything that's 40 or less is valid, so what happens if we give it like a negative 5?
>> All right, okay testing mindset, this is what I love. All right, so I work- 5.

[00:09:00] You're right we said this in our text, but we did not verify that. So I think they're gonna say you owe me some money. Yeah, gross pay is- 75. So if we wanted to enhance this, what can we do based on what we learned in the last section about conditions?

[00:09:24]
>> We could use a double ampersand.
>> That's right, we can use an and so if, so while the hours worked is greater than maximum, I would say or. Or hoursWorked is less than 0, all right let's double check that So we say -5 boom and then I say 45 boom and then let's say 34.

[00:09:59] Perfect, great catch. All right, any other questions? Awesome, all right, let's go back to.
>> Someone said edge case with 0.
>> Edge case 0, you get 0 that's okay, right?
>> You don't work, you don't get paid.
>> You don't work, you don't get paid, boom. So I guess we can change our message though to say, 0 and 40, that'd be fair, all right?

[00:10:32] All right, great. So the wild loops there are controlled by a condition. So the while loop will continue running as long as that condition is true and it also tests the condition before it enters inside of the loop. So every time it runs again, it'll first check if that condition remains true.

[00:10:58] You wanna use a while loop when the code inside of it is conditional and it may or may not need to run, right? Based on some condition.