Can someone explain what is the condition for this for loop?
for(;;) {
//do sth.
}
Can someone explain what is the condition for this for loop?
for(;;) {
//do sth.
}
If the test condition is empty (and it is here), there is no test and the loop continues indefinitely. It's a short form for an infinite loop.
This is an infinite for loop with no condition. The for loop contains following semantics
for(loop variable initialization ; condition to terminate ; variable increment)
Since there is nothing in between then two ';'its no condition infinite loop
It is equal to this:
while(true){
//do sth.
}
which is an infinite loop.
If you tries to decompile this simple program
for(;;){
System.out.println("yes");
}
You will get this one as result:
do
System.out.println("yes");
while(true);
I'm using this decompile tool: JAD Java Decompiler (dont work for Java 8+)
The three expressions of the for loop are optional, an infinite loop can be created as follows:
// Infinite loop
for ( ; ; ) {
// Your code goes here
}
As everyone said, it is an infinite loop. A simple way to see that it is an infinite loop is to look the for(;;)
statement in byte code.
Take this reference class:
public class Test {
public static void main (String[] args){
for(;;){}
}
}
The compiler output (in bytecode):
public class Test {
public Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0:
goto 0
}
The goto 0
jumps at the label 0
, which is above line. This process will never stops.