10
\$\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\$
11
  • \$\begingroup\$ Or something like main();? \$\endgroup\$
    – jimmy23013
    Sep 2, 2015 at 18:08
  • \$\begingroup\$ @jimmy23013 No, but you can do main(null). \$\endgroup\$
    – Ypnypn
    Sep 2, 2015 at 18:14
  • \$\begingroup\$ @Ypnypn Or main(a) if main is declared as public static void main(String[]a). \$\endgroup\$
    – Dennis
    Sep 2, 2015 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\$ Sep 2, 2015 at 23:39
  • \$\begingroup\$ Related: codegolf.stackexchange.com/questions/13152/… \$\endgroup\$
    – Kzqai
    Sep 3, 2015 at 0:24

4 Answers 4

18
\$\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
  • 7
    \$\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, 2015 at 18:14
  • 4
    \$\begingroup\$ you can exchange the empty braces for a semicolon for(;;); \$\endgroup\$ Sep 2, 2015 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, 2015 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, 2015 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\$ Oct 6, 2018 at 23:19
2
\$\begingroup\$

50 bytes

interface M{static void main(String[]a){for(;;);}}
\$\endgroup\$
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
  • 3
    \$\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, 2016 at 11:10
0
\$\begingroup\$
for(/*init*/;;/*stmt*/){/*stmt*/}

To save characters, you can use the init and increment parts of the loop to save a few bytes by using the semicolons.

\$\endgroup\$
2
  • \$\begingroup\$ How is this different than the accepted answer? \$\endgroup\$
    – corsiKa
    Jun 27, 2022 at 2:25
  • \$\begingroup\$ I'm just trying to make it more clear where the statements should go. One more thing: If you do need to use break, you can put it in the condition. \$\endgroup\$
    – Eric Xue
    Jul 2, 2022 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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