- To be able to properly initialize, terminate, and increment a
for
statement. - To be able to use
continue
andbreak
. - To understand one reason why we use methods.
Definition: A
loop
is code that is run over and over again.
Remember a while
loop?
while (condition) {
// do something over and over
}
Definition: A
while
statement repeatedly executes a block of code while a given condition istrue
.
A for
statement is a loop that looks like this...
for (initialization; termination; increment) {
// do something
}
...where:
- The initialization is executed once, beginning the loop.
- When the termination expression is
false
, the loop terminates; this is analogous to the condition in awhile
statement. - The increment expression is executed after each iteration; this can also decrement.
As with if
and while
, the body of the loop contains a single statement.
Demo:
for
loop that prints 0-4 inclusive.
Exercise: Rewrite the following
while
loop as afor
loop:int i = 0; while (i <= 100) { i++; System.out.println(i); }
while
and for
loops can both express the exact same computation.
Definition: syntactic sugar is a syntax designed to make the code easier to read.
What is wrong with this for
loop?
for (int i = 3; i < 30; i--) {
System.out.println("counting... " + i);
}
The hardest part about for
loops is properly stopping the loop. Programmers often make mistakes when setting the termination condition for a loop. In particular, we are often off-by-one. This kind of bug it so common, it has its own Wikipedia page.
Exercise: Counting Machine: Write a program that counts from 0 to a user-specified number.
Exercise: [harder] Counting Machine Revisited: Now let the user input the initial value, the max value, and the increment.
Exercise: "FizzBuzz" is a very famous program, up there with "Hello World!". Write FizzBuzz using a
for
loop.
Remember break
?
Definition:
break
terminates the loop.
Java also has another useful keyword, continue
:
Definition:
continue
jumps to the next iteration of the loop.
continue
can be useful skip over some unnecessary computation.
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0) continue;
System.out.println(i);
}
Is continue
syntactic sugar? What could you do with the above code if Java didn't provide the continue
statement?
Exercise: Simplify your code for FizzBuzz using
continue
.
Often, programmers need to iterate over text. Java provides two functions, String.length()
and String.charAt()
that make iterating over a String
fun. Here is how to use them:
System.out.println("Queens!".length()); // 7
The function String.charAt()
returns the char
at the index provided. For example:
System.out.println("Queens!".charAt(3)); // e
In-class assignment: [hard] Below is a quote from Grace Hopper, the inventor of the first compiler. Write a program that uses a
for
loop to count how many times she says the word "data". You'll need twoString
functions in order to complete this assignment. Make sure to verify your answer.
"We must include in any language with which we hope to describe complex data-processing situations the capability for describing data. We must also include a mechanism for determining the priorities to be applied to the data. These priorities are not fixed and are indicated in many cases by the data.
"Thus we must have a language and a structure that will take care of the data descriptions and priorities, as well as the operations we wish to perform. If we think seriously about these problems, we find that we cannot work with procedures alone, since they are sequential. We need to define the problem instead of the procedures. The Language Structures Group of the Codasyl Committee has been studying the structure of languages that can be used to describe data-processing problems. The Group started out by trying to design a language for stating procedures, but soon discovered that what was really required was a description of the data and a statement of the relationships between the data sets. The Group has since begun writing an algebra of processes, the background for a theory of data processing.
"Clearly, we must break away from the sequential and not limit the computers. We must state definitions and provide for priorities and descriptions of data. We must state relationships, not procedures."