Presentation is loading. Please wait.

Presentation is loading. Please wait.

While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.

Similar presentations


Presentation on theme: "While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement."— Presentation transcript:

1 While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement might not be executed at all. The expression is evaluated again after each execution of the statement until the expression becomes false.

2 The while Statement The while statement has the following syntax:
while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repeatedly until the condition becomes false.

3 Logic of a while Loop condition evaluated false statement true

4 The while Statement Note that if the condition of a while statement is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times A sentinel value indicates the end of the input The variable sum maintains a running sum A loop is used to validate the input, making the program more robust

5 Counter.java The Counter program shown here simply prints the values from 1 to 5. Each turn through the loop prints one values, then increases the counter by one. A constant called LIMIT holds the maximum value that count is allowed to reach. The condition of the while lop, means that the loop will keep going as long as count is less than or equal to LIMIT. Once count reaches the limit, the condition is false and the loop quits. Please pause the video if you need more time to study the loop code.

6 Average.java The Average program shown here reads integer values from the user, adds then up, and computes their average. We don’t know how many values the user may enter, so we need to have a way to show that the user is done. In this program, we pick zero to be a sentinel value, which is a value that shows the end to the input the way a sentinel stands guard the gate of a port or perimeter of an army’s camp. The while loop continues to process input values until the user enters zero. A sentinel value must always be outside the normal range of values entered. In this program a variable called sum is used to keep a running sum which means it is the total of the values entered so far. The variable sum starts at zero, and each value read is added to and stored back into sum. We also have to count the number of values that are entered so that after the loop finishes we can divide by the right number to get the average. Note that the sentinel value is not counted. Please pause the video if you need more time to study the loop code.

7 WinPercentage.java The WinPercentage program computes the winning percentage of a sports team based on the number of games won. We use a while loop this program to validate the input, meaning we guarantee that the user enters a value that we consider to be valid. The while loop keeps executing, repeatedly asking the user for a valid input, until the entered number is indeed valid. Validating input data, avoiding errors such as dividing by zero, and performing other actions that guarantee proper processing are important design steps. We generally want our programs to be robust, which means that they handle errors, even user errors, well. Please pause the video if you need more time to study the loop code.

8 Infinite Loops The body of a while loop eventually must make the condition false If not, it is an infinite loop, which will execute until the user interrupts the program This is a common logical error You should always double check to ensure that your loops will terminate normally

9 Forever.java The programmer must make sure that the condition of a loop will eventually become false. If it doesn’t, the loop will keep going forever, or at least until the program is interrupted. The infinite loop, like the one shown above, is a common mistake. Please pause the video if you need more time to study the loop code.

10 Nested Loops Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop Each time through the outer loop, the inner loop goes through its full set of iterations

11 PalindromeTester.java The code here contains two loops, one inside the other. The outer loop controls how many strings are tested, and the inner loop scans through each string, character by character, until it determines whether the string is a palindrome. Please pause the video if you need more time to study the loop code.

12 Iterators An iterator is an object that has methods that allow you to process a collection of items one at a time The hasNext and next methods are used to loop through the collection Several classes in the Java class library define iterator objects, including Scanner while (myCollection.hasNext()) { System.out.println(myCollection.next()); }


Download ppt "While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement."

Similar presentations


Ads by Google