Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Infinite Loop [duplicate]

Tags:

java

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?

like image 941
Shiladittya Chakraborty Avatar asked Feb 04 '16 07:02

Shiladittya Chakraborty


1 Answers

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).

like image 63
Idos Avatar answered Nov 08 '22 00:11

Idos