ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
loops, cont.
Things to think about, when writing loops
The purpose of a loop statement
is to separate two different tasks:
doing some task (once),
and
controlling the program to do the task over and over.
Before writing a loop, think about what the particular task is,
before you try to do it many times!
Often, it is helpful to write this task as its own separate function,
and just have your loop call that function.
- What pieces of information do you want to know,
the t'th time through the loop?
Make a variable for each such piece of info you'll keep track of.
- What is your ending condition -- are you sure it will be reached?
- How do you compute the (t+1)st value, from the t'th value?
-
Can you state an invariant -- something which is true
every time through the loop?
(Hopefully, this invariant, combined with the ending-condition being met,
will imply that you've computed what you wanted to.)
Loop problems
We spend the week writing more functions involving loops.
source code
loop syntaxes
Here is the syntax of various loop constructs,
as applied to common situations:
|
while loops
|
for
|
for-each
|
counting over
[start,
stop)
|
int index = start;
while (index < stop) {
statements...
index = index + 1;
}
|
This is a bit more awkward than the corresponding for-loop.
|
for (int index = start; index < stop; ++index ) {
statements...
}
|
|
[not applicable]
|
iterators
|
Iterator it = expressioniterator;
while (it.hasNext()) {
variable = it.next();
statements...involving variable
} |
|
for (Iterator it = expressioniterator; it.hasNext(); /* no update */ ) {
variable = it.next();
statements...involving variable
}
|
This is a bit more awkward than the corresponding while-loop.
|
for ( variable : expressioniterator ) {
statements...involving variable
} |
|
general syntax
|
statementinit;
while (condition) {
statements...
}
|
|
for (statementinit; condition; end-of-loop-update) {
statements...
}
|
|
for ( variable : expressioniterator-or-collection) {
statements...involving variable
}
|
This is very natural when you have a iterator or a collection
and you want to process each item, but you don't care
about which order the items are processed in.
This is the one loop where break can be necessary.
|
-
Counting over [start,stop):
doing a task for
start,
start+1,
start+2,
…
stop-1.
As we've seen before, it's handy to use a half-open interval:
- The size of [a,b) is b-a (not b-a+1 or b-a-1 or anything).
In particular, [a,a) is the empty set
(there are no numbers which are ≥a (the left endpoint)
and are <a (the right endpoint).
- The interval [0,100) = [0,50) ∪ [50,75) ∪ [75,100);
we don't need to be constantly twiddling an endpoint by one to get our
intervals to align.
(You can also count down instead of up,
or step by a different amount than 1 each time.)
- iterators:
Iterators are anything with a boolean hasNext() method
and a next() method.
One example is java.util.Scanner
(which has the methods
int nextInt(),
double nextDouble,
and
String nextLine,
in addition to String next(),
as seen in the program for reading a web page).
The for-each loop applies to any Collection as well as any
Iterator, as we saw in the example programs
about File.listFiles().
There is also
the do-while loop, as in the book.
Each of these admit the break statement,
which is handiest when searching using a for-each:
that is, as soon as you find a matching item you want to break out of the loop.
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws