Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use for (ever){} instead of for (;;) in Java [closed]

Tags:

java

I'd like to use an infinite loop in Java: for (;;) and I think it would look amusing if I could replace that ';;' with the variable 'ever' like this:

String ever = ";;";
for(ever){
...
}

Is such a thing possible in Java?

like image 728
Andreas Hartmann Avatar asked Jan 30 '13 13:01

Andreas Hartmann


People also ask

What can I use instead of a for loop in Java?

To answer your question, an alternative would be to use a Set and check that its size is the same as the array length after you inserted all the array's elements into it (or check on the fly). By efficienty you change the complexity of the algorithm from O(n^2) to O(n) but you use (at most) O(n) extra space.

Is there a forever loop in Java?

An infinite while loop in Java is a set of code that would repeat itself forever, unless the system crashes. At a certain point, the data becomes an overload and the program will overflow. This means it will fail.

How do you end an endless loop in Java?

You can break any loop using break; . If your program is already listening for keyboard input, you just need to put that break command inside a conditional statement that checks for the desired input.


1 Answers

No it is not possible.

The closest you can come is this:

final boolean ever = true;
for (;ever;) { }
like image 66
Philipp Wendler Avatar answered Sep 22 '22 21:09

Philipp Wendler