<%@ EnableSessionState=true %> <% Option Explicit dim thisurl thisURL=Request.ServerVariables("PATH_INFO") & "?" & Request.ServerVariables("QUERY_STRING") const title="Java Games tutorial #3" %>
Current area: HOME -> Java Games tutorial #3

Java Games tutorial #3

Do Loops

There a several ways to repeat commands (or "loop") in Java. One is our old friend the while loop. By now, you're probably comfortable with how it works. For instance, if you wanted to print a message to the Java console 10 times, you could use the following while loop:

 

int i = 0;

while (i < 10) {

    System.out.println("My message here.");

    i++;

}

 

The do loop is just another way of performing repeated tasks. Conceptually, it's slightly different that the while loop.

 

Recall that you can think of a while loop in terms of "everyday language" as: "while some condition is true, perform a task".

 

The do loop turns this phrase around slightly, rephrasing it as: "do some specific task while a condition is true."

 

This rearrangement affects the code, making it slightly different than a straight while loop:

 

int i = 0;

do {

    System.out.println("My message here.");

    i++;

} while (i < 10);

 

I can hear you asking, "If these different loops are just different ways of writing the same commands, why bother having two? Why not just use one all the time and throw the other one away?"

 

There are two reasons. First--and most practically--they behave slightly differently in some cases. For instance, suppose, instead of initializing i to 0 in these code samples, we initialized it to 10? Then the while loop becomes:

 

int i = 10;

while (i < 10) {

    System.out.println("My message here.");

    i++;

}

 

In this case, your message will never print, because Java hits

 

while (i < 10) {

 

and sees that is not less than 10, and so jumps to the end of the loop without ever executing the code inside it.

 

Now consider how the do loop behaves under the same initial conditions:

 

int i = 10;

do {

    System.out.println("My message here.");

    i++;

} while (i < 10);

 

Java hits the do command, enters the loop, and prints the message. Only after printing the message does it increment i and perform the test. Meaning that a do loop will always execute the contents of the loop at least once.

 

Secondly--and more conceptually important--sometimes it's just easier to think in terms of one loop or the other. For instance, for our simple "print a message 10 times" case, you may have thought of the solution this way:

 

  1. Set some counter variable to 0.

  2. If the counter is less than 10, print the message.

  3. Increase the counter by 1.

  4. Go back to step 2.

(which implies a while loop)

 

or this way:

 

  1. Set some counter to 0.

  2. Print the message.

  3. Increment the counter by 1.

  4. If the counter is less than 10, go back to step 2.

(which implies a do loop).

 

Neither is better, but depending on how your brain works, you may find that you're more comfortable with one or the other.

 

Now, let's get back to the tutorial.