-1

What does the following statement mean in Java?

while(condition_1) {
    do something...
} while (condition_2);

Is there any real-world situation to use it?

UPDATE: code example

    int i = 3;
    while (i < 10) {
        System.out.println(i++);
    } while (i < 15);
Nelson Tatius
  • 7,693
  • 8
  • 47
  • 70

3 Answers3

10

It's just 2 while statements. There is no significance to the second while being on the same line as the closing bracket of the first while loop. Whitespace, for the most part, doesn't matter. Your code would do the same thing if there were blank lines between the two while statements.

Edit: since you edited your code to include an empty while loop, you should notice that your second while loop doesn't actually do anything other than loop. So if you loop once, you'll be looping forever. You might be confusing this with a do-while loop, in which case you should consult the tutorials: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • 2
    So it prints 3, 4, 5, 6, 7, 8 and 9, and then iterates forever. – Amir Pashazadeh Jan 08 '14 at 19:15
  • Completely right explanation it seems. Also, I feel this is mostly bad practice. Why have a while that does not do anything except maybe through inside the parenthesis. OP should have posted real code. – Menelaos Jan 08 '14 at 19:17
2

It doesn't looks for me any different from:

int i = 3;
while (i < 10) //loop that ends once i == 10
    System.out.println(i++);

while (i < 15) //infinite loop
    ; //that does nothing

There are just two while loops and badly formatted code.

Kosmo零
  • 4,001
  • 9
  • 45
  • 88
1

your second while is making your code sample to go in an infinite loop. for better understanding of do-while please go through the example mentioned here : Java do while, while

Community
  • 1
  • 1
Ashish
  • 1,121
  • 2
  • 15
  • 25