Learn Programming in Java


<<Previous | ToC | Next >>

Lesson #4: Iteration & Termination

Last time we built a simple guessing game program. Did you experiment with changing the approved range? What happened when there were more than 8 possible numbers? We had three identical sequences of question followed by input, wouldn't it be easier if the computer did the same thing over and over again? That's called iteration, or more often a loop, and it's a very important programming tool.

The basic way to tell a Java program to repeat the same thing over and over is called "while" and we give it a condition for doing whatever we want it to repeat. If the condition is true, it does the body of the loop and then looks again, otherwise it goes on to the next statement after the while loop. It's important that somewhere in the body the condition eventually gets turned off, otherwise the program never gets out of the loop.

One of the two most common kinds of loop is where you want to do it a certain number times, and you set a variable to count those times through, then increment the counter inside the body, and test for it's still less than the number of times you want, like this:

int n = 0;
while (n<7) { // repeats a total of seven times, first n=0, then n=1, ... finally n=6
    // do something, possibly involving n
    n++;
} // end of while


This is so common that Java has some "syntactic sugar" (a different way to spell the same thing) that makes your intentions more obvious. Notice that I colored the different parts of the for loop to match their corresponding parts of the while loop structure:

for (int n = 0; n<7; n++) {
    // do something, possibly involving n
} // end of for


You can also use an existing variable and omit the declaration inside the parentheses: "for (n=0; n<7; n++)". It is important to understand that this is only a different spelling for the while loop, you could change the value of the control variable n inside the loop, which would totally mess up the number of times the loop runs, but you must resist the temptation to do that. Better languages than C or Java won't let you change the control variable inside the loop, but this is the real world we live in. You will make mistakes, and Java won't help you with this particular one.

The other common kind of loop is controlled by some outside condition, like whether input has arrived or an error occurred. There is no control variable to increment, but there must be some testable condition, or again the loop never comes out. That's not always bad, sometimes -- especially with a program controlling hardware -- the program never has any reason to stop, so you would code it as "while (true) ..." I do that a lot, but there is another way out of a loop that has not met the exit condition but you still want out now, and that is called "break;" because it breaks out of the loop no matter what. In my programs I write a lot of while(true) loops that exit by means of the break statement. I often use the structure instead of nested if/else statements when my conditions don't nest nicely. In the same place you use break you can instead use continue to jump to the next iteration.

Most programmers think of the braces ("{}") as being part of the obligatory loop structure -- and that's probably a good habit to get into -- but in reality Java expects the while- and for-loop to have a single statement after the right parenthesis, so if that's all you have, then you don't need the braces. The same is true of if and else. The sample code you see from me may or may not have braces, depending on whatever is convenient. Here are some examples of goofy but valid while and for loops:

while (false) ; // null statement executed never
while (false) {} // same thing (sometimes also called a "stub")
for (i=0;i>4;i++) {} // same idea, but nore complicated
for (i=9;i>0;i--) z++; // control variable counts down
while (true) break; // executes once, but breaks out immediately
for (;true;) break; // same idea, as a for-loop


Part of being a good programmer is knowing what differences matter, and what doesn't. For example, sometimes I put the ending right brace on a separate line, sometimes I put it at the end of the last line it encloses (but before any comment). Java doesn't care, but I get to look at more of my program (without scrolling) when there are fewer one-brace lines. On the other hand, it's easier to find missing braces when they are spread out. Some program editors (including BlueJ, when you click to the right of the brace) help you find matching braces.

Now let's look at that guessing game again. Recall there were five lines of code that were exactly repeated three times. One important quality of being a good programmer is knowing when and how to re-use code. Any time you have the same or very similar code showing up in different parts of your program -- or especially in the same place, like here -- we want to re-use a single piece of code in all those places. A loop is one way to do that: throw two of those copies away and re-use the remaining copy three times by putting those five lines into a loop. We could use a for-loop to do the five lines exactly three times, but if you experimented with different value ranges, you can now appreciate why we might not want to do that. Let's set the exit condition of a while-loop to be that the range has been reduced to zero, that is, (hi==lo) which means that we do the loop again so long as (hi>lo) like this:

    int hi = 8, lo = 1, mid;
    char ans;
    System.out.println("Think of a number between " + lo + " and " + hi);
    while (hi>lo) {
      mid = (hi+lo)/2; // initial test @ 4
      System.out.print("Is your number greater than " + mid + " (y/n)? ");
      ans = Zystem.ReadLetter();
      if (ans == 'y') lo = mid+1;
        else hi = mid;
    } // end of questioning while
    if (hi==lo) System.out.println("Your number is " + hi);
      else System.out.println("Oops something went wrong");


As written, it should work the same as it did before. But suppose you make the high end of the range larger, like "hi = 99"? Then it automatically asks its question three or four more times. Do you understand why? You need to convince yourself that you understand why it does what it does. I call it "thinking like a computer," and you will never get to be a good programmer without it. Run it in the debugger if necessary.

Once you have that down cold, we'll try a harder game, Tic-Tac-Toe,

Next: Iteration in a Game

<<Previous | ToC | Next >>

First Draft: 2016 October 17