TopOverloading methods and constructorsFor loops

For loops

Java has two types of for loops. We will talk about the old style of for loop here and then talk about the newer form when we discuss arrays. As we noted earlier in the term, there are a number of while loops in Grace that have the same structure:

var index: Number := initValue
while (endTest) do {
   // do stuff
   index := index + 1
}

In the above, endTest is a boolean-valued expression.

There are many similar while loops in Java with similar structure:

int index = initValue
while (endTest) {
   // do stuff
   index = index + 1
}

Java (and before that C++, and C) introduced a for loop that allows a programmer to combine the first two lines and the last in the body into a single looping statement:

for (int index = initValue; endTest; index++) {
   // do stuff
}

While this looks pretty different from the Grace for loop, it serves the same purpose of iterating through a series of elements (indices in this case) and performing the same operations on each.

A simple example is the following loop that prints out the squares of all numbers from 0 to 9:

for (int index = 0; index < 10; index++) {
   System.out.println{"The value of "+index+" squared is "+(index * index)
}

The body of this loop is executed for the numbers 0, 1, 2, ..., 9, and then the loop terminates.

The print statement in the loop has a couple of interesting points. It concatenates a string, the value of index, another string, and the square of the index, with the result of the concatenation printed. When a value of another type is concatenated with a string, the system attempts to convert that value to a string and then concatenate it. Primitive types like int, double, and boolean have a built-in operation that does this conversion. Object types invoke their method named toString. As a result, you should implement a toString method in classes that you write. If you don't include it, a default (and very unhelpful) version will be inherited from the top of the class hierarchy, Object. Java does not have String interpolation like Grace. That is, I could not write the above as you would in Grace: "The value of {index} squared is {index * index}".

Another point that will come up later is that in Java, like C and C++, we typically start counting at 0 rather than 1. Thus you will often see for loops where the index starts out as 0 rather than 1. If that is the case and you want it to go throughout the loop exactly n times, then you must have the termination condition by index < n rather than using <= n.

The indices in the for loops can start at any value you like as well as go up by any number each time through the loop. They can also have a more complex termination condition. Thus the following loop counts down from n to 1:

for (int index = n; index >=1; index--) {
   ...
}

while the following loop increases through the even numbers, terminating when i exceeds 50 or variable gameOver becomes true.

for (int index = 2; index <= 50 && !gameOver; index:= index + 2) {
   ...
}

TopOverloading methods and constructorsFor loops