5

I saw this bit of code on the interents somewhere. I'm wondering what the do is for.

public class LoopControl {
    public static void main(String[] args) {
        int count = 0;

        do {
            if (count % 2 == 0) {
                for (int j = 0; j < count; j++) {
                    System.out.print(j+1);

                    if (j < count-1) {
                        System.out.print(", ");
                    }
                }

                System.out.println();
            }

            count++;
        }
        while (count <= 5);
    }
}

By which I mean what exactly does do mean? What's its function? Any other information would be useful, too.

L0j1k
  • 12,255
  • 7
  • 53
  • 65
David
  • 14,569
  • 34
  • 78
  • 107
  • 3
    Reformated your code. Oh hey wait - now you can see where the `do` belongs ^^ – Leo Apr 17 '10 at 21:22
  • Can you reformat it again to properly indent the brackets. The second if statement doesn't have an opening bracket, so that bracket that lines up with it actually belongs to the for loop and the next one belongs to the first if. – DaveJohnston Apr 17 '10 at 21:24
  • 2
    @DaveJohnston already noticed myself. That's why you don't use ifs without brackets :D – Leo Apr 17 '10 at 21:28
  • @Mef true, I don't think I have ever written an if statement without brackets and whenever I find it in code I am working on I make a point to change it (so annoying). – DaveJohnston Apr 17 '10 at 21:32
  • it isn't annoying on its own with missing brackets. it is annoying if you forget to put them in when you add an extra line. Fortunately the automatic format on saving in Eclipse (optional) indents so you can at least SEE it quickly. – Thorbjørn Ravn Andersen Apr 17 '10 at 21:51

5 Answers5

6

It is a do-while loop. So it will do everything in the following block while count is less than or equal to 5. The difference between this and a normal while loop is that the condition is evaluated at the end of the loop not the start. So the loop is guarenteed to execute at least once.

Sun tutorial on while and do-while.

Oh, and in this case it will print:

1, 2
1, 2, 3, 4

Edit: just so you know there will also be a new line at the start, but the formatting doesn't seem to let me show that.

DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
3

It is similar to a while loop, with the only difference being that it is executed at least once.

Why? Because the while condition is only evaluated after the do block.

Why is it useful? Consider, for example, a game menu. First, you want to show the menu (the do block), and then, you want to keep showing the menu until someone chooses the exit option, which would be the while stop condition.

João Silva
  • 89,303
  • 29
  • 152
  • 158
2

It's a while loop that gets executed at least once.

Edit: The while and do-while Statements

kloffy
  • 2,928
  • 2
  • 25
  • 34
1

do { ... } while(CONDITION) ensures that the block inside do will be executed at least once even if the condition is not satisfied, on the other hand a while statment will never execute if the condition is not met

Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112
-1

It goes with the while. do { ... } while() is a loop that has the conditon in the end.

Serafeim
  • 14,962
  • 14
  • 91
  • 133