Java Fundamentals

While Loops

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

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.


Transcript from the "While Loops" Lesson

>> 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.

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.

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.

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?

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.

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?

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.

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?

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.

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.

>> 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.

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.

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.

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.

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?

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?

>> 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.

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.

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?

>> 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.

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?

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.

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.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now