140
\$\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\$
13
  • 50
    \$\begingroup\$ I've got to start posting programs with a negative byte count to beat all these empty files! \$\endgroup\$
    – CJ Dennis
    Oct 3, 2015 at 4:32
  • 4
    \$\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, 2015 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\$ Oct 6, 2015 at 13:05
  • 1
    \$\begingroup\$ @ThisSuitIsBlackNot Yes, file input is allowed. \$\endgroup\$
    – user41805
    Oct 6, 2015 at 16:47
  • \$\begingroup\$ Can you print "", an empty string? \$\endgroup\$
    – AAM111
    Mar 7, 2016 at 23:39

553 Answers 553

1
5 6
7
8 9
19
2
\$\begingroup\$

Perl 6, 7 5 bytes

The standard boring one is just an empty loop (;;){} construct.
( spelled for(;;){} in other languages )

loop {} # 7 bytes

There are also exotic ones as well

  • infinite sequence of the Any type object ( default value in $_ )

    .roll(*) # 8 bytes
    
  • unterminated sequence generator (0,1,2,3,4 ... Inf, Inf, Inf, Inf)

    0...* # 5 bytes
    
\$\endgroup\$
2
\$\begingroup\$

Roadrunner, 14 bytes

mEEp mEEP MEEp
\$\endgroup\$
2
\$\begingroup\$

Seriously 0.1, 2 bytes

1W

1 pushes a 1 on the stack, W executes the code between it and the next W (or EOF) while the value on the top of the stack is a truthy value. Since the value on top of the stack stays 1, it NOPs forever.

\$\endgroup\$
2
\$\begingroup\$

BotEngine, 2 bytes

><

Fairly self-explanatory.

\$\endgroup\$
3
  • \$\begingroup\$ do bots loop around? if so, you could just do > \$\endgroup\$ Nov 12, 2015 at 23:34
  • \$\begingroup\$ @anOKsquirrel No, they unfortunately do not. As stated in the specs, bots are destroyed upon exiting the grid. \$\endgroup\$ Nov 12, 2015 at 23:35
  • \$\begingroup\$ ah, I see. better read more \$\endgroup\$ Nov 12, 2015 at 23:47
2
\$\begingroup\$

Marbelous, 7 bytes

00
\\//

One marble bounces between the two deflectors forever.

Alternate version:

00
/\/\
\$\endgroup\$
2
  • \$\begingroup\$ What about simply MB ? \$\endgroup\$
    – overactor
    Dec 7, 2015 at 9:32
  • \$\begingroup\$ @overactor I've been meaning to change the language spec so zero-input boards don't proc every tick and instead wait for one input which they discard. That's a much more useful construct. Regardless, even with the old spec, MB would infinitely recurse, not loop. \$\endgroup\$
    – Sparr
    Dec 7, 2015 at 19:18
2
\$\begingroup\$

Javascript, 8 bytes

while();

This almost crashed my computer once :o

\$\endgroup\$
6
  • \$\begingroup\$ Wait... this works? How? \$\endgroup\$ Nov 28, 2015 at 17:04
  • \$\begingroup\$ I don't know... \$\endgroup\$
    – user46167
    Nov 28, 2015 at 17:05
  • \$\begingroup\$ Where does it work? \$\endgroup\$
    – Qwertiy
    Dec 14, 2015 at 21:32
  • \$\begingroup\$ @Qwertiy chrome \$\endgroup\$
    – user46167
    Dec 14, 2015 at 21:36
  • 1
    \$\begingroup\$ Nope: Uncaught SyntaxError: Unexpected token )(…) \$\endgroup\$
    – Qwertiy
    Dec 14, 2015 at 21:36
2
\$\begingroup\$

Mouse-2002, 2 bytes

()

( begins a loop; ) ends it; there's no ^ so it never breaks.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Lol, cat and mouse :) \$\endgroup\$
    – user41805
    Dec 14, 2015 at 13:57
  • \$\begingroup\$ I think it's pretty great that I have the nickname cat, and am working on rebooting a language called mouse :) \$\endgroup\$
    – cat
    Dec 14, 2015 at 14:15
2
\$\begingroup\$

QBasic, 8 7 bytes

DO:LOOP

Even shorter than the GOTO one!


V1.0

1:GOTO 1

Classical Goto.

\$\endgroup\$
2
\$\begingroup\$

beeswax, 3 or 4 bytes

A real loop (4 byte solution)

*PFJ

Explanation:

* Generate IP, moving to the right.                      [0,0,0]•
P Increment top local stack value.                       [0,0,1]•
F Set all local stack values to top local stack value.   [1,1,1]•
J Jump to [row,column] = [top,2nd] local stack values.   [1,1,1]•
  IP jumps back to to the *

Shorter 3 byte solution

I am not sure if reflecting IPs back and forth counts as loop, but here it is:

j*j

Explanation:

j Mirror IP in horizontal direction
* Create IP

So, this program creates two IPs moving to the right and the left, which are reflected back between both j’s indefinitely.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I suggest you use the 3 byte solution. \$\endgroup\$
    – user41805
    Dec 26, 2015 at 7:35
2
\$\begingroup\$

Spiral, 1 byte

0

All programs start at the label 0. All programs halt at the command !. Self-explanatory.

\$\endgroup\$
2
\$\begingroup\$

Mouse16, 2 bytes

I hereby release the language I've been teasing!

0\

Goes to zero... which is the first byte... which executes the \ goto again... which -- well, you get the idea. This is like 10 GOTO 10 in BASIC.

\$\endgroup\$
2
\$\begingroup\$

Lua, 14 bytes

repeat until a

or

while 1 do end

The first one works because a is nil (because it is undefined), so it will never be true. The second one works because anything that is not false or nil, when casted to a boolean, evaluates to true.

Not boring version, 23 bytes

debug.getinfo(1).func()

debug.getinfo(1) returns the debug information of the current stack, and the func property represents a function that executes that stack. This will call the main stack an infinite amount of times.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Will any of these eventually overflow? \$\endgroup\$
    – cat
    Jan 31, 2016 at 5:08
  • 1
    \$\begingroup\$ @cat I don't think they will. I tested all of them online on this demo and it didn't overflow, or put anything in the output for that matter. \$\endgroup\$ Jan 31, 2016 at 16:18
  • \$\begingroup\$ I just tested on lua5.2.4 and the first two occupy 100% of a core but 0.0% memory and so will never overflow, while the last one crashes with a stackoverflow in a few seconds after eating 20% of memory each second. \$\endgroup\$
    – cat
    Jan 31, 2016 at 16:35
  • \$\begingroup\$ Having said that, debug.getinfo(1).func() on lua5.3 behaves exactly like the other two do. \$\endgroup\$
    – cat
    Jan 31, 2016 at 16:37
  • 1
    \$\begingroup\$ That's good to know, I actually didn't know it overflows on earlier versions of lua. Thanks for letting me know. \$\endgroup\$ Jan 31, 2016 at 18:20
2
\$\begingroup\$

2-ill, 7 bytes

@ @
@$@

The instruction pointer just loops counterclockwise forever.

\$\endgroup\$
2
\$\begingroup\$

Perl, 6 bytes

{redo}

See redo in perldoc.

\$\endgroup\$
2
\$\begingroup\$

Factor, 15 bytes

Yea, Factor is verbose.

[ t ] [ ] while

Do nothing forever. Hangs the Listener.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Here on PPCG we have such an interesting definition of verbose. \$\endgroup\$
    – Cyoce
    Mar 25, 2016 at 6:48
2
\$\begingroup\$

The Infamous Shakespeare Programming Language, 122 bytes

.
Page,a cat.
Ford,.
Act I:
Scene I:z
[Enter Page and Ford]
Page:
Am I better than zero?
Ford:
If so, let us proceed to z.
\$\endgroup\$
2
  • \$\begingroup\$ Aren't the descriptions of the characters in the Dramatis Personae just comments? I'm pretty sure this would just terminate on the first iteration. \$\endgroup\$ Jan 25, 2017 at 2:03
  • \$\begingroup\$ If they evaluate to a valid expression, then they will be evaluated. \$\endgroup\$ Jan 25, 2017 at 22:54
2
\$\begingroup\$

V, 1 byte

ò

V is a 2D string based golfing language that I wrote am in the process of writing, inspired by vim. In vim, recursive macros are a little bit of a pain to set up, so I tried to make them as easy as possible in V. The syntax for a recursive macro is:

ò<code>ò

This is equivalent to

qq<code>@qq@q

in vim.

V will automagically fill in the missing delimiter (in this case, the second ò) so we can leave it off, giving us a nice and simple one-byte solution.

\$\endgroup\$
2
\$\begingroup\$

Subleq bytecode, 3 binary words

00 00 00

Equivalent to

*00 = *00 - *00
if(*00 <= 0) goto 00
\$\endgroup\$
2
\$\begingroup\$

T-SQL, 8 bytes

l:goto l

(Not to be confused with this excellent answer from @MickyT in Standard SQL)

\$\endgroup\$
2
\$\begingroup\$

S.I.L.O.S, 11 bytes

lbla
GOTO a
\$\endgroup\$
2
\$\begingroup\$

S.I.L.O.S, 9 bytes

:a
GOTO a

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Turtlèd, 3 bytes

any character but * works in the middle

[-]

alt:

{*}

Explanation:

[    ] Brackets make a while loop. The character after the opening bracket is taken, and
       the loop is executed while the current cell is not that character.

hence

[-]

Runs whilst the current cell is not -, but will never change it to that value, so infinitely runs, and never ouputs as it only outputs at the end.


{*}

works similarly, but it runs while the current cell IS that value. by default, the starting cell is *, so it runs forever, since it will never change its value

(nontrivial) Polyglot, Turtlèd and Brainf*** 5 or 4 bytes, depending on implementation

doesn't make use of BF non instruction nops.

If you happen to have another cool esolang that might be able to be fitted in, tell me.

+[-+]

In wrapping implementations,

in non-wrapping:

+[+]

Explanation:

+    - essentially a nop in Turtlèd with no string, increments cell in BF
 [+] - loops while current cell is not: {BF:0, Turtlèd:"+"}. increments cell in BF

 [+-] - loops while current cell is not {BF:0, Turtlèd:"+"}. - is nop in Turtlèd with 
        no string, and `+-` together is nop (+1,-1) in BF 
\$\endgroup\$
2
\$\begingroup\$

PHP, 8 26 bytes

set_time_limit(0);for(;;);

Almost forgot: default time limit is 30 seconds and script will exit with a Fatal Error if I don´t unset it.

Run with -r

\$\endgroup\$
2
\$\begingroup\$

J, 7 Bytes

(-^:_)_

A more "readable" form would be (- ^: _) 1. The _ can be any non-zero number and it will work the same (_ represents infinity in J). ^: is the "power" conjunction; it iterates a verb a specified number of times. E.g. (f ^: 3) 0 == f(f(f(0))). When told to iterate _ times, it keeps applying until it produces a constant output. Since negation never reaches a limit, this is an infinite loop.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 2 bytes

[[

Try it online!

Explanation:

[     # Infinite loop start
 [    # Infinite loop start
      # Implicit infinite loop end
      # Implicit infinite loop end

You need something inside the infinite loop, or else it will just end

\$\endgroup\$
1
  • \$\begingroup\$ You don't need anything inside if you close it with ], but it's still 2 bytes ([]). \$\endgroup\$ Nov 12, 2016 at 10:35
2
\$\begingroup\$

Microscript, 2 bytes

1{

Essentially just n=1;while(n!=0){}. The interpreter autocloses any loops, etc. that are left open.

The Microscript II program 1[ is equivalent.

\$\endgroup\$
2
\$\begingroup\$

Scala 12 bytes

while(true)0

Avoid one character using 0 instead of while(true){}

\$\endgroup\$
2
\$\begingroup\$

DUP, 6 bytes

DUP is a dialect of Wouter van Oortmerssen’s FALSE, invented by Ian Osgood.

[1][]#

Explanation:

This uses DUP’s while loop. the first block [1] is the condition block that checks if the condition is true/nonzero, and if it is, executes the second empty block [] that does nothing. The execution block is executed as long as the condition is nonzero.

 [1][]#
 instr.    data stack   return stack
 [        0                         push '[' location
    [     0,3                       push '[' location
      #   0            5,0,3        push '#' and '[' locations on return stack
 [                     
  1       1            5,0,3        push 1 (truthy)
   ][]    0            5,0,3,0      condition true → execute 2nd (empty) block
 [                                  jump to condition block (location 0, popped from the return stack)
  1       1            5,0,3
   ][]    0  ...       5,0,3,0             infinite loop

Just for the fun of it, here is a visually equally long solution that is 8 utf-8 bytes long, but unique to DUP because FALSE lacks this functionality:

[A]⇒AA

Explanation:

         data     return
         stack    stack    operator
[          0                                 push location of open bracket
   ⇒                                         operator assignment to
    A                      A => 0            new operator A (at address 0)
     A                                       execute operator A, push current IP location on return stack
[                 5                          move to operator A at location 0
 A                5,1                        execute operator A, push current IP location on return stack
[                                            move to operator A at location 0
 A                5,1,1                      execute operator A, push current IP location on return stack
...
[
 A                5,1,1,1,1,1,1,1...

As you can see, the latter recursive solution quickly fills the return stack and sooner or later leads to a stack overflow, depending on the available RAM.

A full introduction and explanation of DUP instructions etc. can be found on my GitHub repository or on the pages linked on the online Javascript DUP interpreter webpage.

P.S.: I just noticed that someone already posted a FALSE version. I’m sorry for the duplicate. In this case both languages look the same.

\$\endgroup\$
2
\$\begingroup\$

QBIC, 2 bytes

{}

This compiles into QBasic as DO: LOOP.


Note that not long after answering this challenge, the workings of QBIC has been altered. We now see IF, DO and FOR as 'language constructs': an opening statement, <code goes here> and a closing statement. Note that WHILE/WEND, functions and subs could also be supported as language constructs in the future.

Those currently supported by QBIC (IF, FOR and DO) are opened using ~, [ and { respectively. Closing them can be done with either a ] or a }: these mean 'Close the last language construct' and 'Close all constructs'.

QBIC has had the ability to auto-close language constructs for some time now. The above code could be one byte only: {. The final statement that QBIC adds to its own source is a } to close all constructs.

\$\endgroup\$
2
\$\begingroup\$

Alice, 0 bytes

Try it online!

The empty program in Alice does nothing... in particular it doesn't terminate.

\$\endgroup\$
1
5 6
7
8 9
19

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.