145
\$\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
    Commented 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
    Commented 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\$ Commented Oct 6, 2015 at 13:05
  • 1
    \$\begingroup\$ @ThisSuitIsBlackNot Yes, file input is allowed. \$\endgroup\$
    – user41805
    Commented Oct 6, 2015 at 16:47
  • \$\begingroup\$ Can you print "", an empty string? \$\endgroup\$
    – AAM111
    Commented Mar 7, 2016 at 23:39

570 Answers 570

1
9 10
11
12 13
19
1
\$\begingroup\$

Thue, 11 bytes

0::=0
::=
0

(Silly 30 character minimum)

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

BASIC, 8 bytes

2 GOTO 1

A simple answer, but hey, it uses basic!

Also, for fun:

Brainf*ck, 3 bytes

+[]
\$\endgroup\$
1
\$\begingroup\$

Arcyóu, 6 bytes

(@ 1 1

While 1: append 1. Note that this will blow the stack eventually, since it's slowly building a list of 1s in memory.

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

Go, 32 bytes

surprisingly long. surprisingly, never ends.

package main;func main(){for{}}

Fun fact: compiling this, due to the go tail-call and 03- optimising compiler, this generates an executable with absent .TEXT and .DATA sections.

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

Emacs Lisp, 9 bytes

(while t)

Pretty straight-forward.

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

Befalse, 7 bytes

A two dimensional language.

Extremely straightforward; the ! unconditionally skips the next instruction and the / and \ reflect intuitively.

!/\
 \/
\$\endgroup\$
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 2 chars / 4 bytes

↻;

I won't add a Try it here because you probably don't want to try it.

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

ResPlicate, 5 bytes

1 4 2

After 1 step, this expands to 2 2 2 2, which is the smallest period 1 oscillator. That this is true was once the subject of a lengthy proof, which was thenceforth unceremoniously deleted when I pointed out that the fact was kind of a little bit obvious. I'm sure you can re-derive said proof yourself.

\$\endgroup\$
2
  • \$\begingroup\$ So it's a form of cellular automaton? \$\endgroup\$ Commented Jan 2, 2016 at 1:13
  • \$\begingroup\$ @SuperJedi224 It's closer to being a Post canonical system. It's nearest neighbor in automaton space is probably Self-BCT, though it is not clear if the latter is usable or even Turing-complete. ResPlicate is at least an automaton with only a single state. \$\endgroup\$
    – quintopia
    Commented Jan 2, 2016 at 1:55
1
\$\begingroup\$

STXTRM, 2 bytes

;;

Program execution in this language is... strange.

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

Dirst, 4 bytes

lpn_0

Loops as long as 0 is equal to 0.

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

Sally, 20 bytes

void a a
void main a

Why the extra four bytes? First, I pipe it to sally2c, which transpiles it to C:

#include "sally.h"

/* {void -> void}: */ void apply_a(void)
{
  apply_a();
}

/* {void -> void}: */ int main(int argc, char ** argv)
{
  if(argc <= 0) { fprintf(stderr, "0 values needed"); exit(1); }
  apply_a();
  {
  }
  argv = argv;
  return 0;
}

Then, during compilation, I use the -O2 flag for tail-recursion (otherwise it segfaults), which I believe is necessary for consideration as a language.

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

RPG fixed, 133 bytes

H dftactgrp(*no) actgrp(*new)
H option(*srcstmt:*nodebugio)
H bnddir('QC2LE')
C     A             TAG
C                   GOTO      A

Basically it defines a few headers for it to compile (h-spec) and have to rows of c-specs. If it was written in free format it would have looked like this:

H dftactgrp(*no) actgrp(*new)
H option(*srcstmt:*nodebugio)
H bnddir('QC2LE')
/free
tag a;
goto a;
/end-free

Or in Perl like this:

a:;     # This is a label
goto a; # Goto that label
\$\endgroup\$
3
  • \$\begingroup\$ For those uncommon languages you should always add a link to an interpreter and/or show an example run of it. Otherwise no one can really judge your answer. Also an explanation of your program would improve your answer a lot. \$\endgroup\$
    – Denker
    Commented Feb 16, 2016 at 15:35
  • \$\begingroup\$ @DenkerAffe Link to an interpreter would be hard if not impossible, as rpg is compiled on an IBM mainframe. A link to a wiki could do though. \$\endgroup\$ Commented Feb 16, 2016 at 16:09
  • \$\begingroup\$ Thats fine. Looks a lot better now! \$\endgroup\$
    – Denker
    Commented Feb 16, 2016 at 16:47
1
\$\begingroup\$

Condit, 2 bytes

a 

Note the trailing space. Due to what I believe is a bug, the Python 3 interpreter loops infinitely on unrecognized tokens.

\$\endgroup\$
2
  • \$\begingroup\$ Unless we're using different interpreters, this won't work without a trailing linefeed. \$\endgroup\$
    – Dennis
    Commented Feb 16, 2016 at 18:05
  • \$\begingroup\$ All Unix/GNU utilities will, as their definition of line includes the linefeed. \$\endgroup\$
    – Dennis
    Commented Feb 16, 2016 at 21:45
1
\$\begingroup\$

Puzzlang, 4 bytes

XX X

I have verified that this is, in fact, the shortest possible solution. Equivalent to +[] in BF.

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

Carriage, 7 bytes

#11-\@!

Smallest self-copying program that I could find.

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

DUP, 2 bytes

~!

Try it here.

Found this completely by accident.

Explanation

If the stack is empty, ~ (normally bitwise NOT) just pushes -1 to the stack. !, which is normally lambda execute, pushes 1! to the return stack, which then tells the IP to go back to ~, and the loop goes on.

\$\endgroup\$
1
  • \$\begingroup\$ The quirkster implementation shows a few non-standard behaviors that appear to be implementation errors or maybe specific to Javascript behavior. Cool find, though. Your solution should fill up the return stack rather quickly, so it’s going to crash sooner or later. \$\endgroup\$
    – M L
    Commented Jun 3, 2018 at 1:48
1
\$\begingroup\$

Grond, 7 bytes

w(1){}

Compiler here https://github.com/epicTCK/Grond

\$\endgroup\$
1
  • \$\begingroup\$ This is a very short answer... Well done! \$\endgroup\$
    – wizzwizz4
    Commented Mar 8, 2016 at 16:50
1
\$\begingroup\$

Hoon, 4 bytes

|-
$

The |- rune is a synthetic rune that expands to =<($ |.($)), a macro that expands to a self-calling function. That function returns $, the name of a limb. Gates like |. are essentially "objects" in Hoon, called cores, with a list of limbs. $ is the empty name of the limb containing the code to run to call the gate. The code creates a core with one limb, $, that returns the value at the limb $ (itself) and calls it immediately.

\$\endgroup\$
1
  • \$\begingroup\$ You forgot to make the Hoon, ... a header. \$\endgroup\$
    – user48538
    Commented Mar 8, 2016 at 21:09
1
\$\begingroup\$

Gogh, 1 byte

Another Gogh answer!

Ø

The character Ø infinitely loops all code before it. (from the wiki)

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

Python 3, 100 79 bytes

x=lambda x:x(x)
while x:
    try:x(x)
    except BaseException:pass

Note: this doesn't freeze my IDLE or my machine, nor does it hog lots of memory: it just occupies (more than) a core on an eight-core processor.
Also note: pressing CTRL-C (or whatever you have KbInt bound to) a few times will eventually cause it to error and quit. I could write in protection for that but it's pointless in this case.

\$\endgroup\$
3
  • \$\begingroup\$ You can't use BaseException to catch all exceptions? \$\endgroup\$
    – ASCII-only
    Commented Mar 24, 2016 at 14:18
  • \$\begingroup\$ @somebody I tend to forget BaseException catches KeyboardInterrupt. \$\endgroup\$
    – cat
    Commented Mar 24, 2016 at 14:32
  • \$\begingroup\$ @somebody I couldn't quite tell you why I answered this way in the first place... \$\endgroup\$
    – cat
    Commented Mar 24, 2016 at 14:34
1
\$\begingroup\$

MNNBFSL, 4 bytes

[[<]

A bit complicated. Read the Esolangs page for more information.

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

Bash, 10 bytes

f(){ f;};f
It does end up crashing the program /bin/bash, but I think that's allowed.

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

Pylongolf2, 2 bytes

><

That's it.

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

Jumper, 2 bytes

:0

Move along, nothing to see here but a simple self-GOTO...

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

Sh, 17 bytes

while :;do :;done

Probably the shortest one.
Ungolfed:

while [ : ]; do
:
done

: is a command that always returns 0 (a.k.a. the same as true).


Sh+coreutils, 13 bytes

yes>/dev/null

yes is a program from the coreutils package, that, when not given an argument, continuously outputs y (note the newline), while given an argument, outputs that argument plus a newline continuously. --help and --version display usage and version number, respectively.

\$\endgroup\$
3
  • \$\begingroup\$ Why did you post this as a new answer instead of an edit to this one? Also, your "ungolfed" code doesn't do the same as the golfed code. [ : ] returns 0 because the string : is non-empty, not because it executes :. \$\endgroup\$
    – Dennis
    Commented May 15, 2016 at 17:58
  • \$\begingroup\$ @Dennis I thought that code between [ ] is actually executed. \$\endgroup\$ Commented May 15, 2016 at 18:32
  • \$\begingroup\$ No, that's not what happens. [ is an alias for test. If expression is a single argument, test returns false if the argument is null and true otherwise. \$\endgroup\$
    – Dennis
    Commented May 15, 2016 at 20:59
1
\$\begingroup\$

Parallax Assembly, 0 bytes

The effect if set up correctly leaves the processor core initialized to zeros, which happens to be a nop. Running off the end executes the processor special registers, which also happen to be initialzed to zero except for ina, but the high bits are zero (for some reason this particular CPU floats low rather than high) so it's still a nop, and loops back to the beginning by overflowing the IP register.

Incidentally a blank CPU is already set up correctly so it's just a matter of loading the null program without disturbing the state.

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

Pancake Stack, 71 bytes

Put this # pancake on top!
[a]
If the pancake is tasty, go over to "a".

Yay, self-GOTOs with #pancakes...

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

Codan, 4 bytes

«»

All loops in Codan are infinite if they don't have breaks. This program is transpiled to C as follows:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <termios.h>
int main(void)
{
    int mem[10000];
    int alpha, beta, t;
for (;;) {
}

    return 0;
}
\$\endgroup\$
1
\$\begingroup\$

PRINDEAL, 10 bytes

a t
 t
 t
 t
t

Creates a command named t that calls itself, then calls itself regardless of whether the first call suceeded. An interpreter was posted which does not use recursion, and thus will keep running forever (or until it runs out of memory)

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

Revaver2pi, 5 bytes

TEL 0

TEL searches (cyclically) for the next TEL with the same first argument and jumps to there.

\$\endgroup\$
1
9 10
11
12 13
19

Your Answer

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

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