6

There are a lots of option available for infinite loop but this are mostly use while(true) or for(;;)

I know while(true) is the best option, since it is easier to understand.

But I want to use for(;;).

I want to know what is going on inside of for loop when I we used two ; within for loop.

for(;;)

Semicolon means its an empty statement. But how its works when we use inside of for loop for infinite execution?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • 2
    I don't know the answer to this question. But I do know how to answer it. I would compile a simple java program using the construct and then examine it with a decompiler like javap. I would not be surprised if `while(true)` and `for(;;)` generate the same byte code. I also would not be surprised if they differ. – emory Feb 04 '16 at 07:52
  • @Shiladittya please consider accepting my answer if it solved your question. If not, I would be happy to elaborate further – Idos Feb 05 '16 at 20:22

1 Answers1

3

This is what happens:

for (initialization statement; condition check; increment/decrement)
    // loop body;

With for(;;):

  • There is no initialization.
  • There is no condition check.
  • There is no increment/decrement.

Therefore it will run forever, exactly like while(true).

Idos
  • 15,053
  • 14
  • 60
  • 75