44

What is the convention for an infinite loop in Java? Should I write while(true) or for(;;)? I personally would use while(true) because I use while loops less often.

Justin
  • 24,288
  • 12
  • 92
  • 142

5 Answers5

84

There is no difference in bytecode between while(true) and for(;;) but I prefer while(true) since it is less confusing (especially for someone new to Java).

You can check it with this code example

void test1(){
    for (;;){
        System.out.println("hello");
    }
}
void test2(){
    while(true){
        System.out.println("world");
    }
}

When you use command javap -c ClassWithThoseMethods you will get

  void test1();
    Code:
       0: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #21                 // String hello
       5: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: goto          0

  void test2();
    Code:
       0: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #31                 // String world
       5: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: goto          0

which shows same structure (except "hello" vs "world" strings) .

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 2
    I think it's time for Java to come up with an infinite loop construction so we don't need workarounds, like other languages have. May I suggest `do {}` without `while` being required, or simply `loop {}`. – Oliver Hausler Nov 04 '15 at 18:55
  • @OliverHausler I'd prefer `forever`, like e.g. in Scratch https://wiki.scratch.mit.edu/wiki/Forever_%28block%29 – Frank Schmitt Apr 28 '16 at 10:37
15

I prefer while(true), because I use while loops less often than for loops. For loops have better uses and while(true) is much cleaner and easy to read than for(;;)

AJMansfield
  • 4,039
  • 3
  • 29
  • 50
06needhamt
  • 1,555
  • 2
  • 19
  • 38
  • 1
    Another argument in favor of while( true ) is maintainability: true may become a logic condition in the future – Aubin Apr 13 '13 at 15:52
  • 5
    @Aubin OTOH, `for(;;)` is more pleasant when golfing – John Dvorak Apr 13 '13 at 15:53
  • 3
    @Aubin, so can the middle part of the `for`! Just stick it in like `for(; condition(););`. Using a `for` also means you could easily add a variable to count iterations of the loop (or other 'between iterations' stuff) after the second of the two semicola. – AJMansfield Nov 11 '13 at 21:39
  • 1
    There's nothing "hard to read" about `for...` its a three letter word. – ChiefTwoPencils Oct 20 '14 at 00:34
9

It's up to you. I don't think there is a convention for such a thing. You can either use while(true) or for(;;)

I would say I encounter more often while(true) in the source codes. for(;;) is less often used and harder to read.

TheEwook
  • 11,037
  • 6
  • 36
  • 55
6

for(;;) sucks, it is completely unintuitive to read for rookies. Please use while(true) instead.

Community
  • 1
  • 1
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
5

Ultimately, it is your choice. The Java tutorials show both for and while.

However, while(true) is used more often as it's far more understandable.

Michael
  • 41,989
  • 11
  • 82
  • 128
blackpanther
  • 10,998
  • 11
  • 48
  • 78