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
  • 51
    \$\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
7 8
9
10 11
19
2
\$\begingroup\$

Braingolf, 4 bytes

[1+]

Try it online!

Braingolf v2, 3 bytes

1[]

Try it online!

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

Pure CPython 3.10 Bytecode, 2 bytes

Hex:

71 00

Disassembly:

    >>    0 JUMP_ABSOLUTE            0 (to 0)

Attempt This Online!

\$\endgroup\$
2
  • \$\begingroup\$ I know all instructions are supposed to be 2 bytes long, but this still works if you remove the zero byte. Does the runner code somehow pads this or does JUMP_ABSOLUTE work without an argument? \$\endgroup\$
    – ovs
    Oct 30, 2021 at 10:04
  • 1
    \$\begingroup\$ @ovs that would mean the bytecode interpreter is reading one byte past the end of the bytes object's buffer, and the byte after the end happens to be a 0 byte. This probably works most of the time, but it's too far into undefined behaviour IMO \$\endgroup\$
    – pxeger
    Oct 30, 2021 at 10:16
2
\$\begingroup\$

KonamiCode, 12 bytes

v(^)L(>)B(>)

S(^)L(>)B(>) does the same thing but isn't shorter.

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

Plumber, 7 bytes

[]
[[]]

This is the shortest possible infinite loop in Plumber.

Plumber programs are divided into units, which are always two characters wide (and padded to two with spaces if shorter). This one consists of three units: [], [[, and ]]. When a Plumber program is run, a 0 packet is dropped from all [] on the top row. This packet can be picked up by a ][, which pushes it to the sides.

A [[ will push it to the right, but it continues down (and is destroyed upon leaving the 2d space). Likewise, when the ]] is pushed into from either side (left in thus case), the packet is dropped and destroyed, and pushed back to the left. The [[ pushes it back to the ]], and this continues indefinitely.

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

Lost, 1 byte

?

Try it online!

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

Rust, 6 bytes

loop{}

Try it online!

That's one very fast loop

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

Knight, 3 bytes

W1N

Epic W1N.

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

Swift, 12 bytes

while(1>0){}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ What about while(1){}? \$\endgroup\$
    – user63571
    Jan 30, 2017 at 17:58
  • 1
    \$\begingroup\$ The condition needs to be a Bool, so while(1) wouldn't compile. You probably can save 1 byte by removing the parens around the condition: while 1>0{} \$\endgroup\$ Aug 7, 2019 at 10:56
1
\$\begingroup\$

Rust, 17 chars

fn main(){loop{}}

Nothing much interesting to see here.

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

FlogScript, 6 bytes

This creates a string containing code for duplicating the top value on the stack, then popping it and executing it as code. Then it is duplicated, popped, and executed as code.

{.~}.~
\$\endgroup\$
1
\$\begingroup\$

Lua, 11 bytes

In Lua, you can set labels that can be used with the goto statement!

::y::goto y
\$\endgroup\$
1
\$\begingroup\$

HALT, 7 bytes

1 SET 1

This set's the pointer to 1, this is run forever because there is no HALT; command. This will bybass fail-safes to prevent infinite looping.

Try This

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

CoffeeScript, 6 bytes

loop 0

Interesting fact thanks to Martin Büttner, not sure if there's any practical use to it though.

Previous attempt (8 bytes):

1while!0

There is only a while loop, no for (though there are for..in and for..of).

\$\endgroup\$
1
  • \$\begingroup\$ CoffeeScript actually has a keyword for infinite loops: loop 1 \$\endgroup\$ Oct 2, 2015 at 19:19
1
\$\begingroup\$

Basilisk, 5 bytes

:A1gA

Pretty simple.

Explanation

  • :A

Defines position A.

  • 1gA

Pushes one and goes to position A in code. Since the gA pops the top value in stack, if the loop were ever to end, it would not print anything.

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

STATA, 10 bytes

while 1{
}

Apparently for loops in STATA always halt, but while loops can be infinite.

\$\endgroup\$
4
  • \$\begingroup\$ Is the line break needed? I've never used STATA \$\endgroup\$ May 12, 2016 at 20:14
  • 1
    \$\begingroup\$ @AlbertRenshaw The line break is required, and it will throw a syntax error if it is not there. See stata.com/manuals13/pwhile.pdf#pwhile for details. \$\endgroup\$
    – bmarks
    May 17, 2016 at 14:19
  • \$\begingroup\$ What about a: goto a? :D 9 bytes —— stata.com/manuals14/m-2goto.pdf \$\endgroup\$ May 17, 2016 at 17:42
  • \$\begingroup\$ In-fact, the space after a: might not be needed, I don't have STATA so I can't test but I'd imagine it's possible to omit the space and get it down to 8 bytes \$\endgroup\$ May 17, 2016 at 17:44
1
\$\begingroup\$

VBScript, 17 bytes

do
loop while 1
\$\endgroup\$
1
\$\begingroup\$

C++, 15 bytes

Same as C:

main(){main();}
\$\endgroup\$
2
  • 6
    \$\begingroup\$ stackoverflow.com/a/2532922 says "The C++ Standard says that you may not call main() from your own code." \$\endgroup\$
    – Neil
    Oct 3, 2015 at 20:50
  • 3
    \$\begingroup\$ PPCG says "if your code works on some environment, it's fine", and it works for me. A standards-complying solution would be int main(){for(;;);}, I think. \$\endgroup\$
    – Lynn
    Oct 4, 2015 at 19:21
1
\$\begingroup\$

F#, 14 bytes

while 0<1 do()

Self-explanatory.

\$\endgroup\$
2
  • \$\begingroup\$ Also works in Standard ML \$\endgroup\$
    – Demi
    Oct 11, 2015 at 0:05
  • \$\begingroup\$ Nice one. I thought doing it via recursion would be shorter, but this beats it by a byte. =) \$\endgroup\$
    – Roujo
    Feb 15, 2016 at 22:26
1
\$\begingroup\$

AHK, 4 bytes

loop

quick and simple

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I'm going to disagree with this answer here as this won't execute persistently without a #persistent directive present or a Hotkey (which forces a #Persistent) defined in your script. . AHK is a bracket based language, your loop has no defined brackets and will simply exit after the first iteration, in fact your code won't even perform a single loop. This answer is just wrong. \$\endgroup\$
    – errorseven
    Nov 28, 2015 at 20:21
  • 1
    \$\begingroup\$ @ahkcoder wrong,add a msgbox under it to see it does run forever \$\endgroup\$ Nov 28, 2015 at 20:37
  • 2
    \$\begingroup\$ Exactly, you have to add code in order for it execute correctly. Your program is an incomplete implementation, therefore wrong. \$\endgroup\$
    – errorseven
    Nov 28, 2015 at 20:45
1
\$\begingroup\$

Pip, 3 bytes

W1x

Basically a while 1: 0 answer.


A more interesting 2-byte solution that doesn't quite fit the rules:

Vf

f is the current function; V evaluates it. This theoretically goes on forever, but in actuality it ends up causing infinite recursion in the interpreter, which very quickly exits with

Fatal error: maximum recursion depth exceeded while calling a Python object

Oh well.

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

Fourier, 3 bytes

1()

Repeats until the result of the loop equals 1, which it never does.

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

Aheui, 0 bytes

Aheui is a Befunge-like, and the empty program is an infinite loop for exactly the same reason.

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

Batch, 3 bytes

@%0

\$\endgroup\$
4
  • \$\begingroup\$ This produces output. Why not use @%0? \$\endgroup\$
    – Neil
    Oct 3, 2015 at 20:54
  • \$\begingroup\$ @Neil Thanks, I used your suggestion. \$\endgroup\$ Oct 5, 2015 at 10:48
  • \$\begingroup\$ You forgot to update the number of bytes. (The editing restrictions suck, otherwise I would have suggested the edit.) \$\endgroup\$
    – Neil
    Oct 5, 2015 at 10:59
  • \$\begingroup\$ This is at least \$7\$ bytes, as for the file to be executed, its name needs to end in .bat or .cmd. Assuming the filename is just .bat or .cmd, that means a \$4\$ byte file name and so a \$3+4=7\$ byte answer. \$\endgroup\$
    – Makonede
    May 11, 2021 at 20:47
1
\$\begingroup\$

CFL 2 (ComeFrom 2), 17 bytes

10 comefrom 20
20 

Try it here.

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

Carriage, 30 bytes

111-@11-~!$11111++++11-~@11-~!
\$\endgroup\$
1
\$\begingroup\$

SmallTalk – 18 bytes

[true]whileTrue:[]
\$\endgroup\$
1
\$\begingroup\$

C, 16 bytes

main(){for(;;);}

A simple C loop construct. A for loop does not require curly brackets. 

\$\endgroup\$
2
  • \$\begingroup\$ Is there a particular dialect of C that lets you omit the parenthesis after main? GCC throws an error if I try to compile this. \$\endgroup\$
    – Mark
    Oct 5, 2015 at 7:25
  • \$\begingroup\$ @Mark Whoops, I missed that. Editing \$\endgroup\$
    – awd123
    Oct 5, 2015 at 12:04
1
\$\begingroup\$

Poslin, 20 bytes

[ .true ! | ]while !

This does the part between | and ] as long as the part between [ and | returns the true value.

.true ! creates the true value at compile time.

[ .true & | ]while !

works just the same, but here the operation .true is called on every iteration.

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

Prelude, 3 bytes

1()

Any other non-zero digit could replace the 1. This is essentially the same as the Brainfuck solution (just adding it for completeness).

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

PHP, 10 8 bytes

The for(;;) solution has been posted enough, Time for this one:

while(1)

while(1){}

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You're missing a trailing ;. This will produce a parse error. \$\endgroup\$ Nov 15, 2015 at 9:51
1
7 8
9
10 11
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.