144
\$\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\$
14
  • 52
    \$\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

566 Answers 566

0
\$\begingroup\$

Java, 50 bytes

interface D{static void main(String[]a){for(;;);}}
\$\endgroup\$
0
\$\begingroup\$

Copy, 10 bytes

copy 0 0 1

Copy the current instruction at the next location.

Basically a Core war imp.

\$\endgroup\$
0
\$\begingroup\$

LI, 2 bytes

R0

Usually, LI programs (under the current interpreter) always take in input. No input is commonly represented as falsy input, i.e. 0; however, in order to truly accept "no" input, I need to provide my own input to the Recurse function.

Explanation:

R   Recurse with input:
 0  Literal 0

LI, 1 byte

If we're a bit more lax with the input requirements, simply R will work. It doesn't error if you don't give it an input, but that's simply because, while it interprets the input as invalid (empty), it doesn't try to use its value.

\$\endgroup\$
0
\$\begingroup\$

ABCR, 2 bytes

5x

Explanation: 5 loops while the front member of queue B resolves to a truthy value. The default value for queue B is 1, so the loop continues until it finds its matching x; since there are no operations in the loop, it continues ad infinitum with no output.

\$\endgroup\$
0
\$\begingroup\$

Lolo, 20 bytes

lOLolo loLOlo loloLO

You can also put in l at the end, but it won't ever get called.

\$\endgroup\$
0
\$\begingroup\$

Sinclair ZX-81 Basic, 6 bytes

1 RUN

This would take 6 bytes in RAM (2 bytes for the line number, 2 bytes for the line length, 1 byte for the RUN token and 1 byte for a newline).

The way it works is (essentially) the same as the 10 GOTO 10, the first line in the program causes the program to be executed.

\$\endgroup\$
0
\$\begingroup\$

dotsplit, 4 bytes

jump

jump: pops number from stack, moves back that many commands

When the stack is empty, popping results in zero. Hence, it will jump on to the jump command.

In keeping with design principles, the problems this has will not be addressed, but perhaps a new command will be added, called loop-safe or something

\$\endgroup\$
0
\$\begingroup\$

CMD, 2 bytes

Must be in a file named a.bat or a.cmd

@a

Must be in a bat or cmd file

@%0
\$\endgroup\$
2
  • 2
    \$\begingroup\$ The first answer counts as 3 bytes: 2 for the code, and 1 for the restriction on the filename \$\endgroup\$
    – user41805
    Nov 12, 2016 at 11:37
  • \$\begingroup\$ Why? He's being punished for a restriction imposed on him by the system, which only recognises specific suffices as executable code. This seems extremely unfair, because it's completely out of his control. He would not be punished if he used a simple batchfile-editor which would execute the code for him, and thus this punishment is undeserved. \$\endgroup\$
    – z0rberg's
    Jan 5, 2017 at 18:30
0
\$\begingroup\$

Visity, 0 bytes

(no code)

As this answer points out Visity wraps around producing an infinite loop.
Or more conventional (2 bytes):

[]
\$\endgroup\$
0
\$\begingroup\$

Unary, 695 bytes

695 zeros. Program that prints the actual code can be found here.

\$\endgroup\$
0
\$\begingroup\$

7, 3 bits, represented as 1 byte on disk (language postdates challenge)

1

Try it online!

This is fairly simple. The program starts by pushing 7 onto the stack (which is what 1 does); then when the end of the program is reached, the top nonempty stack element is executed, without removing it from the stack (although any empty elements above it are removed). 7 just pushes an empty stack element to the stack, which immediately gets removed as it's the end of the program, and so nothing has changed and the same stack element just gets run again, repeatedly.

\$\endgroup\$
0
\$\begingroup\$

Pari/GP , 9 Bytes

while(1,)             

(dummy text such that SE accepts the answer)

\$\endgroup\$
0
\$\begingroup\$

PKod - 1 byte

Version 1.1 was released very recently as I still update this, which implements this new function/command

<
Explanation: Jumps back to first byte in code. So it jumps back to itself. Normally meant to be used with test cases (e.g.: if prime, jump back to start - iterate all code again, but since thread asks for infinite loop, why not)

Alternative version, 4 bytes:

-+ia
Explanation: -+ , remove then add 1 to value. Thus value keeps jumping from 0 and 1. "i" jumps two blocks back in code until value is next char after "i". Thus until char is 1. Thus until value reaches ascii code of a which is 97. Which never happens
\$\endgroup\$
0
\$\begingroup\$

Chip, 5 bytes

*S

+3 for -w on the command line, which allows the program to run without reading from stdin. Instead, it uses an internal source (generating 0x00 repeatedly by default).


* is a constant high signal; this turns on adjacent elements.
S when powered, suppresses output for the current cycle.

\$\endgroup\$
4
  • \$\begingroup\$ I believe the first flag (-w) is considered 3 bytes (but the following flags will be considered 1 byte each) \$\endgroup\$
    – user41805
    Jan 30, 2017 at 17:12
  • \$\begingroup\$ You are right! I wasn't certain to start with, and your comment forced me to do research on the accepted norms. \$\endgroup\$
    – Phlarx
    Jan 30, 2017 at 17:22
  • \$\begingroup\$ Do you need the -w flag? The consensus is that you can assume STDIN is empty. \$\endgroup\$ Jul 10, 2017 at 0:08
  • \$\begingroup\$ @Challenger5 The problem here is that, by default, Chip terminates when it has processed all of stdin. Empty stdin means that Chip will terminate immediately. \$\endgroup\$
    – Phlarx
    Jul 10, 2017 at 15:14
0
\$\begingroup\$

SmileBASIC, 5 bytes

EXEC.

. is the same as 0.0, so it runs EXEC 0, which executes the program stored in slot 0, which is the default slot.

Example use:

PRINT TIME$
WAIT 1
EXEC.
\$\endgroup\$
0
\$\begingroup\$

BitCycle, 3 bytes

>1<

Places a 1 bit on the playfield and infinitely moves it back and forth between the > < devices.

\$\endgroup\$
0
\$\begingroup\$

MOO, 30 28 26 bytes

while(!suspend(0))endwhile

The suspend(0) is necessary to avoid "task ran out of ticks'

\$\endgroup\$
2
  • \$\begingroup\$ Do you need the spaces? \$\endgroup\$ Jul 9, 2017 at 23:48
  • \$\begingroup\$ No. I don't. Good point. \$\endgroup\$ Jul 9, 2017 at 23:49
0
\$\begingroup\$

Unreadable, 25 bytes

'"""""'"""'""""""'"""'"""

Try it online!

Explanation

'"""""                       While X is true, do Y
      '"""                   [X] - Return 1
          '""""""            [Y] - Set variable X to value Y
                 '"""           [X] - Return 1
                     '"""       [Y] - Return 1
\$\endgroup\$
1
  • 4
    \$\begingroup\$ Explanation? Also, you haven't formatted it correctly, it should be '"""""'"""'""""""'"""'""". \$\endgroup\$ Jul 9, 2017 at 23:48
0
\$\begingroup\$

Nhohnhehr, 11 bytes

+-+
|$|
+-+

Try it online!

\$\endgroup\$
0
\$\begingroup\$

jq 1.5, 13 bytes

until(null;1)

this also works but is no shorter:

repeat(empty)
\$\endgroup\$
0
\$\begingroup\$

4, 12 bytes

3.6147281494

Try it online! (Note: The interpreter used by TiO raises an error with this code. This is caused by the interpreter, not the program itself)

Explanation (According to the wiki specifications):

3.         Start program
6 14 72    Set memory cell 14 to 72
8 14       While cell 14 != 0
9          End while
4          End program
\$\endgroup\$
2
  • \$\begingroup\$ Do you have an interpreter on which this program works? \$\endgroup\$
    – FlipTack
    Nov 16, 2017 at 7:36
  • \$\begingroup\$ @FlipTack I think this should work, but I can't test it right now. \$\endgroup\$
    – KSmarts
    Nov 16, 2017 at 15:12
0
\$\begingroup\$

Hodor, 22 bytes

hodor............(1){}

Hodor!

Or, (44 bytes)

hodor............(hodor. rhodor!? hodor? ){}
\$\endgroup\$
0
\$\begingroup\$

Smalltalk-80, 8 bytes

(or more)

First there is no such thing as a program in Smalltalk-80, just write a snippet anywhere in a text pane and press the doIt! pop up menu. But we can eventually write snippets that don't stop.

[]repeat

is the obvious infinite loop

thisContext restart

is our kind of goto solution

|x|
x:=[:b|b value: b].
x value:x

is a block closure evaluating itself recursively, but since a closure close over local variables, we can do it shorter:

|b|(b:=[b value])value

Since Smalltalk-80 don't do recursive tail elimination, this infinite loop will starve memory and won't really be infinite.

We also may retry code that raised an exception, if ever the exception was temporary...
(a costly variant of repeat)

[0halt]on:Halt do:[:e|e retry]

We can also play with recursive structures, modulo recursive tail elimination problems:

|x|(x:=1->0)key:x;hash

We can also exploit weaknesses of the interpreter itself - this is in latest Squeak 6.x (you'll have to kill the image):

|c|c:=Class new.c superclass:c.c new

Since new will invoke self basicNew initialize, the interpreter will loop in #initialize method lookup. This time no memory growth, the loop is solid.

There must be some shorter exploit, but I'll stop here, Smalltalk is more talk than small for golfing...

In a certain sense, the interpreter performs an infinite loop itself, so doing nothing is already doing an infinite loop, but that's cheating.
In the case of Smalltalk though, since we save the state of the interpreter in an image, and later resume an image at each restart, we're just running the infinite loop for more than 20 years now (or one of its avatars). Since it's ran over a virtual machine, it even perpetuates across different machines, OS, architectures, so it's a quite robust loop!

\$\endgroup\$
0
\$\begingroup\$

Operation Flashpoint scripting language, 4 bytes

@0>1

@ waits until the condition (0>1) is true, which obviously never happens, so it keeps looping and checking the condition forever.

Alternative solution (10 bytes):

#l
goto"l"

This usually freezes the game.

\$\endgroup\$
0
\$\begingroup\$

Implicit, 1 byte

(

Creates a jump point. When the interpreter hits EOF and there is an open jump point, it loops forever.

\$\endgroup\$
0
\$\begingroup\$

REXX, 10 bytes

s:signal s

Of course, the canonical way is a whopping 14 bytes:

do forever
end

Or, depending on implementation:

do forever
nop
end
\$\endgroup\$
0
\$\begingroup\$

Pyt, 3 bytes

1`ł

Explanation:

1             Pushes 1 onto the stack
 `ł           Loops while the top of the stack is not zero

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Acc!!, 21 bytes

Count i while i+1 {
}

Try it online!

In Acc!!, looping is costly. Whitespace between brackets is required and the statement is a bit wordy. This is pretty self-explanatory, but just in case, it increments i until i - (i + 1) = 0, which is never.

\$\endgroup\$
1
  • \$\begingroup\$ This is super late, but Count i while 1 works for -2 bytes. \$\endgroup\$
    – DLosc
    Oct 20, 2021 at 20:52
0
\$\begingroup\$

Forked, 0 bytes



Same as the top two answers. Try it online!

\$\endgroup\$
0
\$\begingroup\$

Momema, 4 bytes

j0j1

Try it online!

Explanation

j  0  #  label j0: jump past label j0 (no-op)
j  1  #  label j1: jump past label j0 (re-execute this instruction)
\$\endgroup\$

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.