The Java while and do while loops

While and Do While
Syntax, semantics and
examples
Copyright © 1998-2011, Curt Hill
The two simple loops
• Either the while or do while is
simpler than the for
• However, all loops need all the
pieces:
–
–
–
–
Initialization
Test
Body of the loop
Advancement
• From a programming perspective
very similar
Copyright © 1998-2011, Curt Hill
Syntax of while
• The form is:
while(condition)
one statement
• While is a reserved word
• Condition is a boolean of the same
type seen elsewhere
• The one statement is any statement
– Most often a compound statement
– Unlike the for the while almost never
has an empty statement here
Copyright © 1998-2011, Curt Hill
Syntax of do while
• The form is:
do
one statement
while(condition);
• Do is also a reserved word
• Condition is a boolean of the same
type seen elsewhere
– Evaluated at end of loop
• One statement is most often a
compound statement
Copyright © 1998-2011, Curt Hill
Flowchart Representation
Leading decision loop
such as a while
Trailing decision loop
such as a do while
Copyright © 1998-2011, Curt Hill
Why Both?
• The while is used instead of a for
when the advancement issue is
fuzzy
• The do is used when the initialization
becomes part of the loop body
– The do is the least used loop
• Never the less, the experienced
programmer uses all three in their
code
• Let’s consider some examples
Copyright © 1998-2011, Curt Hill
While Revisited
• Suppose a cash register machine
program
• We take in the purchase price and
the amount the customer has given
us
• Compute the change
• Make the change in terms of
numbers of bills and coins the teller
has to give
• Try first part of this with a while
Copyright © 1998-2011, Curt Hill
Cash Register Program
double charge, tender;
… // get charge, tender
double change = tender-charge;
int twenties = 0;
while(change >= 20.00){
change -= 20;
twenties++;
}
String s;
if(twenties>0){
s = “Give them “ +twenties+ “
twenties.”;
}
Copyright © 1998-2011, Curt Hill
Example Revisited
• The loop would be duplicated for
tens, fives, etc.
• Both the while and for are leading
decision loops
– They may execute the body of the loop
zero times, if the condition is false
– This is most common form of loop
– This is desirable here, we may not give
them one of some bill denomination
• Leading decision loops must
initialize before the first test
Copyright © 1998-2011, Curt Hill
Another Situation
• Sometimes we end up in duplication
in initialization and body of loop
• Consider the case of reading in the
age of a hospital patient, that we
attempt to verify
int age;
age = scan.nextInt();
while(age > 120){
System.out.println(
“Bad age entered”);
age = scan.nextInt();
}
Copyright © 1998-2011, Curt Hill
Another Loop
• This type of situation may be best
handled with a trailing decision loop
• This checks the condition at the end
of the loop rather than the beginning
• Particularly handy for when the
initialization is part of the loop
processing
• Consider this with a do while
Copyright © 1998-2011, Curt Hill
Consider Example Again
• Get a Hospital patient’s age:
int age;
do {
System.out.println(
“Enter age”);
age = scan.nextInt();
}
while(age > 120);
The loop reads and verifies
Copyright © 1998-2011, Curt Hill
One last thought
• Some loops are predictable and
others not
• For example:
for(int i = 0;i<100;i++)…
for(Pixel ap: p.getPixels())
…
• Prior to the first execution of these
two we can say precisely how many
times they will execute
• These two Copyright
are deterministic
© 1998-2011, Curt Hill
In contrast
• The do while:
do {
System.out.println(
“Enter age”);
age = scan.nextInt();
}
while(age > 120);
– Is unpredictable
• The cash register while is also nondeterministic
Copyright © 1998-2011, Curt Hill
Determinism
• Counting loops usually deterministic, use
a for
• Most often use whiles or do whiles for
non-deterministic loops
• There are several other things we think
about with loop choice as well
• However the while and for are equivalent
in that any for can be converted to a while
and the reverse
Copyright © 1998-2011, Curt Hill
Conclusion
• There is more to learn about the
three loops
• There are also more presentations:
– Logical loop types
– Using break and continue
Copyright © 1998-2011, Curt Hill