From the course: Java Essential Training: Syntax and Structure
While loop - Java Tutorial
From the course: Java Essential Training: Syntax and Structure
While loop
- [Instructor] Loops are structures that cause a block of code to repeat. In most cases, when you need your code to repeat the same statement more than once, it's best to enclose them within a loop. This eliminates the redundancy of copying and pasting those same statements over and over again. Let's look at a problem that will require a loop to solve. Each store employee makes 15 US dollars an hour. Write a program that allows a manager to enter the number of weekly hours worked for each employee and calculate their pay. Do not allow for overtime. We'll need to calculate the salary for employees based on how many hours they have worked. This one says our program cannot allow for overtime, which means we need to validate the input. In this GrossPayInputValidator program, we know that the payRate is $15 and the maxHours is 40. We ask the employer to enter the number of hours the employee worked this week. We read that input in and store it in the hoursWorked variable. Finally, we go ahead and calculate what the gross would be and print that out. However, since our program is not supposed to allow overtime, we have to verify that what the user has entered is valid input. We could check to make sure that hoursWorked is not greater than 40, however, what if they enter the wrong input again? Or a third time or a fourth time? How will we know once it's valid? We could accomplish this by putting the block of code that reads and validates input inside of a while loop. So after we get the input, let's go ahead and say validate input here. To create a while loop, we write the word while and then inside of a set of parentheses, we add a condition. If this condition is true, it will go inside of the loop, which is denoted by a set of curly braces. So what condition should cause us to go inside of the loop? Well, we can say if the hoursWorked is greater than the maxHours, then we'll go inside. This means the input that they've provided is greater than what's allowed, so we want to loop the following statements. So inside of the loop, let's go ahead and let the user know that what they provided was invalid, and so we can say invalid entry. Your hours must be between 1 and 40. Try again. And then we'll update our hoursWorked variable with the input that they provide. Once the enclosed statements have been executed, the program will exit the loop and return to the top of the loop to reevaluate the condition to determine if it needs to execute again. hoursWorked has been updated, so now its new value will be compared to maxHours. If the condition is still true, meaning they enter something greater than 40 yet again, it will go back inside of the loop. hoursWorked will be updated again. It'll come back to the top of the loop to test the condition again. Once the user inputs valid data, this condition will become false and the loop will not be entered. Instead, the program will continue. Let's test our program. So we can enter 56, which is obviously greater than 40 and notice, we get the error message. How about let's try 43. Yep, still the error message and then let's try one under 40 and this time, we exited the loop and our print out is here. An important note is that if you're using variables within the condition of your loop, it's essential that the value of at least one of those variables has the possibility of being updated within the loop. Otherwise the condition could always result in true, therefore causing what's called an infinite loop, meaning the loop will run over and over again until the program eventually crashes. The key points about while loops are that they are controlled by a condition and will continue to run while that condition remains true. The condition is tested before the loop is entered, so there's a chance that the loop will never execute. And it's best to use a while loop when you may or may not need to repeatedly execute a block of code based on the state of the condition.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.