8
\$\begingroup\$

I'm working on a golf for Java and I need an infinite loop. Obviously, I don't want to spend any more bytes than I have to, especially in such an expressive language.

Assuming I have some code I want to run, obviously the baseline is set at while(1>0)/*stmt*/ or 10 additional characters for a single line and while(1>0){/*stmt1*//*stmt2*/} or 12 additional characters for multiple lines. I say additional because the code I want to loop forever (well, forever-ish... I may or may not want to break out of or return from the loop) will have a certain length, and then I must tack on additional characters to make it actually loop.

At first I thought this was the best I'd get, but I figured I'd throw it out to the experts to see if they can find a better one.

\$\endgroup\$
  • \$\begingroup\$ Or something like main();? \$\endgroup\$ – jimmy23013 Sep 2 '15 at 18:08
  • \$\begingroup\$ @jimmy23013 No, but you can do main(null). \$\endgroup\$ – Ypnypn Sep 2 '15 at 18:14
  • \$\begingroup\$ @Ypnypn Or main(a) if main is declared as public static void main(String[]a). \$\endgroup\$ – Dennis Sep 2 '15 at 18:15
  • \$\begingroup\$ Out of interest do Java compilers or JITs typically optimize tail-call recursion, or are these recursions limited by stack size? \$\endgroup\$ – Steve Jessop Sep 2 '15 at 23:39
  • \$\begingroup\$ Related: codegolf.stackexchange.com/questions/13152/… \$\endgroup\$ – Kzqai Sep 3 '15 at 0:24
14
\$\begingroup\$
for(;;){}

It works cause no condition evaluates to always true. Insert the code between the braces.

If you only got a few statements you can place them inside the head (the (;;) part. That the loop still runs is caused by when the condition statement is not a boolean it counts as always true. Thanx to @Ypnypn for the reminder

\$\endgroup\$
  • 6
    \$\begingroup\$ If you have multiple statements, try to put all but one in the for loop itself. e.g., for(;;foo(),bar(),baz())quux(); \$\endgroup\$ – Ypnypn Sep 2 '15 at 18:14
  • 2
    \$\begingroup\$ you can exchange the empty braces for a semicolon for(;;); \$\endgroup\$ – ratchet freak Sep 2 '15 at 22:35
  • \$\begingroup\$ @ratchetfreak Only if you don't intend to break the loop, but the OP that they do want the ability to break. \$\endgroup\$ – Alex A. Sep 3 '15 at 0:14
  • \$\begingroup\$ @ratchetfreak Yes I'm looking for both single and multi-line, although it's debatable whether Ypnypn's suggestion might actually be smaller. Either way, I should edit my post to make my objective more clear. \$\endgroup\$ – corsiKa Sep 3 '15 at 2:07
  • \$\begingroup\$ @Ypnypn I don't think that's legal syntax. There's no comma operator in Java (though it can be used in variable declarations). \$\endgroup\$ – Tom Hawtin - tackline Oct 6 '18 at 23:19
1
\$\begingroup\$

Java - 60 bytes (complete code)

I'm probably not interpreting the question correctly, but this code when compiled and run results in an infinite-ish loop, in that technically it should run forever, but usually it will exhaust the stack memory.

public class X{public static void main(String[]a){main(a);}}

On Coding Ground it ends quite quickly, on my Windows 7 laptop with advanced virtual memory management, it runs a long time, just making the system slower and slower.

\$\endgroup\$
  • 1
    \$\begingroup\$ problem with your solution is, it will give you quite quickly StackOverflowError so it hard to call it infinite loop \$\endgroup\$ – user902383 Feb 12 '16 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.