119
\$\begingroup\$

Your task is to create the shortest infinite loop!

The point of this challenge is to create an infinite loop producing no output, unlike its possible duplicate. The reason to this is because the code might be shorter if no output is given.

Rules

  • Each submission must be a full program.
  • You must create the shortest infinite loop.
  • Even if your program runs out of memory eventually, it is still accepted as long as it is running the whole time from the start to when it runs out of memory. Also when it runs out of memory, it should still not print anything to STDERR.
  • The program must take no input (however, reading from a file is allowed), and should not print anything to STDOUT. Output to a file is also forbidden.
  • The program must not write anything to STDERR.
  • Feel free to use a language (or language version) even if it's newer than this challenge. -Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language. :D
  • Submissions are scored in bytes, in an appropriate (pre-existing) encoding, usually (but not necessarily) UTF-8. Some languages, like Folders, are a bit tricky to score - if in doubt, please ask on Meta.
  • This is not about finding the language with the shortest infinite loop program. This is about finding the shortest infinite loop program in every language. Therefore, I will not accept an answer.
  • If your language of choice is a trivial variant of another (potentially more popular) language which already has an answer (think BASIC or SQL dialects, Unix shells or trivial Brainf**k-derivatives like Alphuck), consider adding a note to the existing answer that the same or a very similar solution is also the shortest in the other language.
  • There should be a website such as Wikipedia, Esolangs, or GitHub for the language. For example, if the language is CJam, then one could link to the site in the header like #[CJam](http://sourceforge.net/p/cjam/wiki/Home/), X bytes.
  • Standard loopholes are not allowed.

(I have taken some of these rules from Martin Büttner's "Hello World" challenge)


Please feel free to post in the comments to tell me how this challenge could be improved.

Catalogue

This is a Stack Snippet which generates both an alphabetical catalogue of the used languages, and an overall leaderboard. To make sure your answer shows up, please start it with this Markdown header:

# Language name, X bytes

Obviously replacing Language name and X bytes with the proper items. If you want to link to the languages' website, use this template, as posted above:

#[Language name](http://link.to/the/language), X bytes

Now, finally, here's the snippet: (Try pressing "Full page" for a better view.)

var QUESTION_ID=59347;var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk";var OVERRIDE_USER=41805;var answers=[],answers_hash,answer_ids,answer_page=1,more_answers=true,comment_page;function answersUrl(index){return"//api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(index,answers){return"//api.stackexchange.com/2.2/answers/"+answers.join(';')+"/comments?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){answers.push.apply(answers,data.items);answers_hash=[];answer_ids=[];data.items.forEach(function(a){a.comments=[];var id=+a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id]=a});if(!data.has_more)more_answers=false;comment_page=1;getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){data.items.forEach(function(c){if(c.owner.user_id===OVERRIDE_USER)answers_hash[c.post_id].comments.push(c)});if(data.has_more)getComments();else if(more_answers)getAnswers();else process()}})}getAnswers();var SCORE_REG=/<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}function process(){var valid=[];answers.forEach(function(a){var body=a.body;a.comments.forEach(function(c){if(OVERRIDE_REG.test(c.body))body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,});else console.log(body)});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)lastPlace=place;lastSize=a.size;++place;var answer=jQuery("#answer-template").html();answer=answer.replace("{{PLACE}}",lastPlace+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link);answer=jQuery(answer);jQuery("#answers").append(answer);var lang=a.language;lang=jQuery('<a>'+lang+'</a>').text();languages[lang]=languages[lang]||{lang:a.language,lang_raw:lang,user:a.user,size:a.size,link:a.link}});var langs=[];for(var lang in languages)if(languages.hasOwnProperty(lang))langs.push(languages[lang]);langs.sort(function(a,b){if(a.lang_raw.toLowerCase()>b.lang_raw.toLowerCase())return 1;if(a.lang_raw.toLowerCase()<b.lang_raw.toLowerCase())return-1;return 0});for(var i=0;i<langs.length;++i){var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;width:500px;float:left}#language-list{padding:10px;padding-right:40px;width:500px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table>

\$\endgroup\$
  • 41
    \$\begingroup\$ I've got to start posting programs with a negative byte count to beat all these empty files! \$\endgroup\$ – CJ Dennis Oct 3 '15 at 4:32
  • 3
    \$\begingroup\$ This challenge is interesting because it brings out lots of 0 byte languages (some of which are NOT esolangs). FWIW, most declarative languages have an implicit infinite loop because declarative languages don't have loops in their syntax (they assume they're running in an infinite loop). Ladder diagrams are perhaps among the oldest such languages. Then you have the Instruction Language (IL), a sort of assembly for PLCs that also assume an infinite loop. ILs, like assembly are different between manufacturers \$\endgroup\$ – slebetman Oct 5 '15 at 9:36
  • \$\begingroup\$ Are programs that read and execute their own source code allowed, or does file I/O break the "must take no input" rule? \$\endgroup\$ – ThisSuitIsBlackNot Oct 6 '15 at 13:05
  • 1
    \$\begingroup\$ @ThisSuitIsBlackNot Yes, file input is allowed. \$\endgroup\$ – Cows quack Oct 6 '15 at 16:47
  • \$\begingroup\$ Can you print "", an empty string? \$\endgroup\$ – OldBunny2800 Mar 7 '16 at 23:39

459 Answers 459

1
\$\begingroup\$

Lithp, 27 bytes

 ((def x #::((next x)))(x))

Define a function, x, which uses tail recursion (via next) to call itself. Then call function x.

This program will never finish. You can't even CTRL+C out of it since keyboard input isn't handled in Node.js whilst in a while loop.

If one were to provide the -d flag to run.js, they would see the function calling itself over and over, never finishing nor running out of stack space.

This was a neat feature to get working in my language. Typically one would use some sort of logic flow to determine whether to return a value or tail recurse back into the current function (using next or recurse.)

The implementation of such tail recursion was fairly simple, but its implications in my functional language are great. My feature set is getting nearer and nearer to Erlang's every week. And that's fun.

\$\endgroup\$
1
\$\begingroup\$

tinylisp, 13 bytes

((v(d f(q(L(f

Parenthesis autocompletion really helps!

We define a function f that calls itself using tail-call recursion. Functions in tinylisp are lists containing two elements: parameter list and expression. Here, our parameter list is L (which makes this a variadic function: L is bound to a list of all arguments) and our expression is (f) (calling the function with no arguments).

((v(d f(q(L(f))))))
         (L(f))      The function list
       (q      )     Quoted to prevent evaluation
   (d f         )    Define f to be that list
 (v              )   The d call returns the name f; evaluate it to get the function itself
(                 )  Call the function

Use Ctrl-C to halt execution.

\$\endgroup\$
1
\$\begingroup\$

uBASIC, 7 bytes

1GOTO1

Note trailing newline. Trivial BASIC variant.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 7 Bytes

Do:Loop

Note: the above freezes Excel - the shortest Excel VBA loop that does not do this is Do:DoEvents:Loop (16 Bytes)

\$\endgroup\$
1
\$\begingroup\$

GNU Sed, 3 bytes

Unlike this answer by @Digital Trauma (which relies on bug #21250: sed: empty label for :/b/t commands), does only use documented Sed commands, in the same byte count.

Golfed

G 
D

Explained

G       #Append a newline to the contents of the pattern 
        #space, and then append the contents of the hold
        #space to that of the pattern space.

D       #Delete text in the pattern space up to the 
        #first newline, and restart cycle with the 
        #resultant pattern space, without reading a new 
        #line of input.
\$\endgroup\$
1
\$\begingroup\$

x86 Assembly, 2 bytes

x: loop x

Bytes: Ô■

Hex: ['0xe2', '0xfe']

\$\endgroup\$
  • \$\begingroup\$ This will not produce an infinite loop. As soon as CX is 1 it will stop looping. \$\endgroup\$ – 640KB Aug 21 at 19:43
1
\$\begingroup\$

tcl, 7 bytes

vwait v

Explanation

\$\endgroup\$
1
\$\begingroup\$

Forte, 4 bytes

1REM

Without an END statement, Forte programs loop infinitely. The empty program is an error, so we leave a comment.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Nice job understanding Forte (even if all you did was write 4 characters in it) \$\endgroup\$ – Esolanging Fruit Jul 10 '17 at 0:06
1
\$\begingroup\$

OIL, 1 byte

6

TIO does not currently support OIL.

Explanation:

6 // Jump to cell A.
0 // Implicit 0.
\$\endgroup\$
1
\$\begingroup\$

Samau, 3 bytes

`xx

Explanation

`x      push the quoted function [x]
  x     execute it without popping it 

It works like the Mathematica program #0[]&[], but Samau doesn't have an iteration limit.

\$\endgroup\$
1
\$\begingroup\$

Lua and RBX.Lua, 14 bytes

while 1 do end

Pretty self explanatory. While 1 is true, it will iterate through the loop.

\$\endgroup\$
1
\$\begingroup\$

Decimal, 6 bytes

91D91D

Pretty simple:

91D   ; declare jump 1
91D   ; goto jump 1

Try it online! I mean, not exactly try it, but... you know what I mean.

\$\endgroup\$
1
\$\begingroup\$

Braingolf, 4 bytes

[1+]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Fission 2, 1 byte

R

Try it online!

Spawns an atom moving right, wraps on the end of the line, R simply sets the atom's direction to right after the program has started

\$\endgroup\$
1
\$\begingroup\$

Lean Mean Bean Machine, 3 bytes

O
~

Marble spawns at O, drops to ~, which teleports it back up to the top of the column.

\$\endgroup\$
1
\$\begingroup\$

Taxi, 22 bytes

[a]Switch to plan "a".

Try it online!

Not very exciting. TIO will only run it for 60 seconds before timing out.

\$\endgroup\$
1
\$\begingroup\$

Emojicode, 28 bytes

🏁🍇🔁👍🍇🍉🍉
\$\endgroup\$
1
\$\begingroup\$

Java, 23 bytes

enum L{L;L(){for(;;);}}

Works with Java 5 and 6. Not with later versions, not with earlier versions. Just another variation of my "Hello, World!" answer.

Proof of correctness

$ java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
$ cat L.java
enum L{L;L(){for(;;);}}
$ javac L.java
$ java L

Then it hangs.

\$\endgroup\$
1
\$\begingroup\$

Ruby w/J-uby, 5 bytes

:~!~0

J-uby is all about making procs (i.e., anonymous functions/lambdas) work more concisely in Ruby. It allows symbols to be called like procs without a call to to_proc. Additionally, it adds a lot of functionality to procs. The added functionality we are using here is !~, or "iterate until constant". Basically, it takes a proc and an initial argument, and applies that proc to the argument until the application stops changing the argument (f.(x) == x). So we are calling ~ (bitwise negation) on 0 until they are equal. But due to the nature of bitwise negation (~x != x, ~~x == x for all x), this never occurs. Infinite loop.

7 bytes:

~:^^~:^

A more "readable" form would be (~:^) ^ (~:^). In J-uby, :^ is the call function; f^x == f[x]. The ~ operation turns a two-argument function into a one-argument function which applies the given argument to both sides. So (~:^)^x == x^x. Finally, we apply this to both sides by turning these into lambdas and we get: ->(x){ x ^ x } ^ ->(x){ x ^ x }. This is analogous to (x => x(x))(x => x(x)) in JavaScript, (\x->x x)(\x->x x) in Haskell, or (λx.xx)(λx.xx) in Lambda Calculus.

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 9 bytes

while(1);

Infinite while loop using 1 as a true value

\$\endgroup\$
  • \$\begingroup\$ It seems I have been beaten by 1 byte \$\endgroup\$ – xDest Nov 10 '17 at 20:44
1
\$\begingroup\$

Java (OpenJDK 8), 50 bytes

interface J{static void main(String[]a){for(;;);}}

Try it online!

Since Java 8 interfaces can have method bodies. As they are always public, we save 3 bytes.

\$\endgroup\$
1
\$\begingroup\$

Husk, 2 bytes

ƒI

Try it online!

Explanation

This is the Haskell equivalent of the following

fix :: (a -> a) -> a
fix f = f $ fix f

fix id

where ƒ is fix and I the identity function (id).

\$\endgroup\$
  • \$\begingroup\$ achieves the same. \$\endgroup\$ – ბიმო Dec 3 '17 at 1:01
1
\$\begingroup\$

Aceto, 1 byte

O

O jumps to the origin of the script. Alternative solutions: Any of v^<>NSEW;§j@

\$\endgroup\$
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 10 bytes

L	:(L)
END

Try it online!

Line L is just a goto L, which works perfectly.

\$\endgroup\$
1
\$\begingroup\$

Aceto, 1 byte

O
O jumps back to the origin, which conveniently is where the O is!

Try it online!

I didn't see @L3viathan's post so...

Some multi-byte solutions

  • @1$
  • 1§uu
  • 1§##

  • 3§ _
    | |
    _

Try It Online!

\$\endgroup\$
1
\$\begingroup\$

Swift, 11 bytes

while 0<1{}

Works both compiled and as a shell script.

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – 0 ' Feb 12 '18 at 10:53
  • \$\begingroup\$ Thanks, @0 '! I'm starting to explore CG-possibilities in Swift despite of its annoying "Expression is too complex" :) \$\endgroup\$ – Dan Karbayev Feb 13 '18 at 11:26
1
\$\begingroup\$

Clojure, 9 bytes (old 15 bytes) thank to @Dennins

(while 1)
\$\endgroup\$
  • 1
    \$\begingroup\$ A simple (while 1) would work as well. Try it online! \$\endgroup\$ – Dennis Feb 13 '18 at 2:51
  • \$\begingroup\$ @Dennins, upvoted \$\endgroup\$ – NTCG Feb 13 '18 at 3:08
  • 1
    \$\begingroup\$ That was intended as a golfing suggestion; feel free to edit it into your post. \$\endgroup\$ – Dennis Feb 13 '18 at 3:33
1
\$\begingroup\$

ORK, 132 bytes

There is such a thing as a y.
A y can z.

When a y is to z:
I am to loop.

When this program starts:
I have a y called X.
X is to z.

Try it online!

Defines a class y with a member function z that does nothing but loop. Then instantiates y and calls its z function.

\$\endgroup\$
1
\$\begingroup\$

VBA 7 Bytes

Do
Loop

I like VBA; it does exactly what you tell it

\$\endgroup\$
1
\$\begingroup\$

Gol><>, 0 bytes

Try it online!

Just for completeness. This works just like zero-byte Befunge infinite loop.

\$\endgroup\$

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.