News Ticker

Examples of infinite for loops

The first two loops continue without stopping because the test condition is true. NOTE: any code after either of these two for loops will not be reachable and therefore will not compile.

[sourcecode language=”java” toolbar=”false”]
for (int x=0;;x++){
System.out.println(x);
}
[/sourcecode]

[sourcecode language=”java” toolbar=”false”]
for (int y=0; true ;y++){
System.out.println(y);
}
[/sourcecode]

[sourcecode language=”java” toolbar=”false”]
for ( ; ; ) {
// your code goes here
}
[/sourcecode]

[sourcecode language=”java” toolbar=”false”]
for ( ; true ; ) break;
[/sourcecode]

Leave a Reply

%d bloggers like this: