-
Motivation
-
As well as having to make choices in programs, it is necessary to have
repetition
-
This allows us to do the same thing over again. Why? Examples from real
life
-
(Cooking) keep on beating until the egg whites are stiff
-
(Grocery) continue looking at apples until you find a good one
-
(Sport) pitch until the other team is out

-
Problem: How would we print the numbers between 1 and 5?
System.out.println (1);
System.out.println (2);
System.out.println (3);
System.out.println (4);
System.out.println (5);
-
What would happen if we wanted to print numbers between 1 and 1000?
-
Example of Java while statement
Should be able to identify conditional part (continuing condition),
and the loop body. If the continuing condition is true, the loop body is
executed. After that is executed, the condition is checked again. If the
continuing condition is false, the statement after the loop is executed.
while (counter <= 1000) { while (condition) {
System.out.println (counter); body
counter += 1;
} }
System.out.println ("Finished");
-
Summing a list of numbers
While statements are good when you don't know how many times the loop
is going to be repeated, but you know the condition that will cause
you to stop.
-
e.g., reading lines in from a file, we want to stop when the end of the
file is reached
-
e.g., reading in numbers from the keyboard, until there are no more
How do we know when there is no more input?
-
the BasicDataReader provides a method, eof, that returns true if there
is no more input, but false if there is more input (eof stands for End
Of File).
How do we sum a list of numbers?
initialize sum to 0
while there are more numbers
read the next number
add it to the sum
Things we now know:
-
what a while statement looks like
-
how to test for no more input
-
how to read a number
-
how to add it to another number
BasicDataReader keyboard = new BasicDataReader ();
int sum = 0;
int inputNumber;
while (!keyboard.eof ()) {
inputNumber = keyboard.readInt ();
sum += inputNumber;
}
Recall that !keyboard.eof() will be true if keyboard.eof()
is false (i.e., the end of the file hasn't been reached)
Running the program:
-
to indicate eof, enter ^Z in DOS, ^D in Unix
Problems:
-
What happens if we don't enter a number, but enter a character by mistake?
The BasicDataReader provides two useful methods for discovering
whether a mistake has been made:
-
fail () - returns true if the data entered was "bad"
-
badData () - returns a string containing the bad data
-
We can thus modify the above loop to be resilient to bad data
while (!keyboard.eof ()) {
inputNumber = keyboard.readInt ();
if (keyboard.fail ()) { // The thing typed wasn't and int!
System.out.println ("Bad data ignored: " + keyboard.badData ());
} else {
sum += inputNumber;
}
}
Unfortunately, there is still another problem - end of file is not a valid
number, but it is valid input for this program!
Need add another if statement:
if (!keyboard.eof ()) {
if (keyboard.fail () { ... }
else {...}
}
-
Exercise
Modify the above program to calculate the average of the numbers entered.
-
Patterns
Reading from a file and processing elements is a common task. We can
use this type of loop in many cases:
while (need to continue) {
read next element
process the element
}
All we need to know:
-
the continuing condition
-
how to read the next element
-
what to do to process it
Note that the text book describes another common loop pattern.
-
Testing loops
-
The loop above can be executed an arbitrary number of times, and so we
cannot test for every case.
-
Care must be taken when constructing loops
-
Things we should test:
-
The case when the loop is never executed (the condition is false the first
time through)
-
The loop body is executed once
-
The loop body is executed more than once
-
Infinite loops are loops that never terminate.
-
Program sits there and appears to do nothing, or doesn't appear to stop
-
Will occur if you don't get the continuing condition right - it may never
return false!
- Example source code