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
    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

568 Answers 568

1
6 7
8
9 10
19
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\$
2
\$\begingroup\$

Triangular, 3 bytes

\/<

Triangular is my first attempt at a two-dimensional esoteric language. Code is formatted into the smallest possible triangle - in this case, a triangle of size two. The IP starts moving Southeast from the top of the triangle.

The above code arranges into this triangle:

 \
/ <

Commands:

  • \ direct IP Southeast (towards the <)
  • < direct IP West (towards the /)
  • / direct IP Northeast (towards the \)
\$\endgroup\$
3
  • \$\begingroup\$ I think you mean a triangle of size two. \$\endgroup\$
    – Leaky Nun
    Jun 16, 2017 at 16:51
  • \$\begingroup\$ @LeakyNun Oh... duh. \$\endgroup\$
    – MD XF
    Jun 16, 2017 at 16:52
  • \$\begingroup\$ 2 bytes with ,/ \$\endgroup\$
    – squid
    Jun 3, 2019 at 15:52
2
\$\begingroup\$

Thotpatrol, 78 bytes

📡JACKING IN📡
🤷 👐 a.txt
🇺🇸REPORT UNPATRIOTIC ACTIVITY🇺🇸

A simple self recursive program that points the interpreter to call itself recursively. The assumption is that the program is in a file called "a.txt" The call is embedded in a try statement to suppress stack overflow warning. The structure is as follows:

function a:
    try a

Link to implementation: https://github.com/MindyGalveston/thotpatrol-

\$\endgroup\$
2
\$\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\$
2
\$\begingroup\$

Piet, 2 codels

Can be interpreted and executed using, for example, PietDev, by either

  • setting the canvas' width to 2, height to 1 and coloring the codels yourself or
  • clicking on "Open", setting the codel width to 25 and uploading the above image.

Click the step button repeatedly to see the program rotate and loop or the run button to freeze the page.

Edit: Try It Online

Edit 2: Just saw that someone already beat me to it.

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

Retina, 10 bytes

I'm pretty sure this can be made shorter.


0
+T`d`10
\$\endgroup\$
9
  • \$\begingroup\$ Can't you just do + followed by nothing? \$\endgroup\$ Oct 2, 2015 at 19:06
  • \$\begingroup\$ @kirbyfan64sos + loops until the input no longer changes. \$\endgroup\$ Oct 2, 2015 at 19:07
  • \$\begingroup\$ I know. But, if you're replacing nothing with nothing, the input will never change, right? \$\endgroup\$ Oct 2, 2015 at 19:08
  • \$\begingroup\$ Ah, nevermind. I was thinking backwards. \$\endgroup\$ Oct 2, 2015 at 19:09
  • \$\begingroup\$ @kirbyfan64sos Exactly; therefore it would only loop once. \$\endgroup\$ Oct 2, 2015 at 19:09
2
\$\begingroup\$

Forth, 21 bytes

Unfortunately, you can't make a word that calls itself without using the keyword recursive, so I use a standard infinite loop instead.

: f begin 0 until ; f

Try it online

\$\endgroup\$
1
  • \$\begingroup\$ Errors with stack overflow on gforth. What did you test this on? \$\endgroup\$
    – Maya
    Mar 6, 2018 at 14:20
2
\$\begingroup\$

beeswax, 3 bytes

O_O

or alternatively

j_j

or

>_<

All three create two bees that get reflected back and forth indefinitely.

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

Z80 Machine Code, 1 Byte

0x76

Halt

When a software HALT instruction is executed, the CPU executes NOPs until an interrupt is received (either a nonmaskable or a maskable interrupt while the interrupt flip-flop is enabled)..

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to PPCG! Btw, if you want to improve existing answers, just leave a comment with your shorter version. \$\endgroup\$
    – ovs
    Jun 4, 2018 at 9:43
  • \$\begingroup\$ Thanks. I have not found answer with Halt for Z80. I'm not sure that this answer with reset is correct for this challange \$\endgroup\$
    – mazzy
    Jun 4, 2018 at 9:52
  • \$\begingroup\$ Oh, I actually meant this unrelated answer which you tried to edit \$\endgroup\$
    – ovs
    Jun 4, 2018 at 9:55
2
\$\begingroup\$

Pepe, 6 bytes

REEReE

Try it online!

Begin loop, end loop. No need to explain it much.

Also, for fun: REE ReE reeE makes the permalink #FBI.

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

Commodore BASIC V2 (Commodore 64/VIC-20), 6 tokenized BASIC bytes

 0 GOTO

or to save some typing (not actual bytes):

 0G[shift]O

which uses the keyword abbreviation for GOTO (may also be typed as GO TO)

In Commodore BASIC V2, GOTO assumes GOTO 0. Byte count is determined from PRINT FRE(0) on VIC-20, default value is 3581.

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

Keg, 1 byte

{

Auto completes the while loop and runs it infinitely

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

Pyramid Scheme, 41 bytes

   ^
  / \
 /do \
^-----^
-^    -
/1\
---

Try it online!

Almost a straight translation of @JoshuaTaylor's Lisp ten-byter.

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

PHP, 12 bytes

Different answers are posted for PHP which use loops or labels, but here is one which doesn't use any of them. Save the file with a single character name and then include itself. For example if you save the file as 1:

<?include 1;

Run with: php -n 1, this will go on until PHP goes out of memory or max execution time reaches.

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

I like frog, 13 bytes

frog like i 

frog like says that it is going to based off the parameters, go back 1 instruction (which is this instruction, creating a loop) i is the parameter saying how many instructions to go back.

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

Marbelous, 8 bytes

@0
00
@0

Marbelous is a language based on marble machines

  • @n (n from 0 to Z) is a portal which teleport the marble to another portal with the same value
  • 00-FF initiate a marble with this value

interpretor

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

AWK, 15 bytes

BEGIN{for(;;)1}

The smallest code that doesn't look for input.

I tried without the 1 and AWK wasn't happy.

Try it online!

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

Forth (gforth), 15 bytes

[begin] [again]

Try it online!

Shortened nonForgivingJesus's answer using square brackets for looping directly.

My first Forth answer!

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

C#9, 8 bytes

for(;;);

In C#9, they introduced Top-Level Statements which means one source file no longer needs any boilerplate.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to the site, nice first answer! This is the shortest C# code I've seen in a while :p \$\endgroup\$ Nov 23, 2020 at 20:13
2
\$\begingroup\$

Python 3, 14 bytes

{*iter(int,1)}

Try it online!

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

C(gcc, platform-independent), 17 bytes Highschooler here, finally something I can do.

main(){for(;;){}}

I read somewhere that this is the least bytes for an infinite loop, although I'm not sure.

\$\endgroup\$
3
  • \$\begingroup\$ The least bytes is a 0-byte C program compiled with GCC-EFI. \$\endgroup\$
    – Makonede
    May 11, 2021 at 20:49
  • \$\begingroup\$ I believe you can replace the {} for the loop body by a single semicolon ; \$\endgroup\$
    – ecm
    Dec 25, 2021 at 16:29
  • \$\begingroup\$ @Makonede wait THAT was possible? \$\endgroup\$ Dec 26, 2021 at 13:54
2
\$\begingroup\$

Jellyfish, 5 bytes

\>1
1

Try it online!

jellyfish doesn't have implicit output, so nothing is printed here.

Explanation

given with positions.

(0,0) \ iterate the following function: 
(1,0)  >  increment 
(0,1) 1 on one
(2,0) till 1 is reached.
\$\endgroup\$
2
\$\begingroup\$

x86 machine code - 2 bytes

 6                                  .loop:
 7 00000000 EBFE                        jmp     .loop

This is a same code as :

 6 00000000 EBFE                        jmp     $

x86_16 machine code - 2 bytes

 12 0005  EB FE              JMP     $

Tested on DOSBox

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Fun fact: Typing this in Unicode yields ëþ. These letters (AFAIK) don't have a common language, but pronouncing them how they normally would be in their respective languages yields "yohth", which sounds sort of like a British person with a lisp trying to say "yours". :P \$\endgroup\$
    – Makonede
    Jun 8, 2021 at 21:41
2
\$\begingroup\$

Locksmith, 4 bytes

0156

0 pushes the next digit (1) to the stack, 5 acts as a label, and 6 jumps to the most recent 5 if the top of the stack is non-zero

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

Knight, 3 bytes

W1N

Try it online!

# pretty straightforward
WHILE 1
    # The official name for this builtin is NULL.
    # Because builtins are determined by the
    # first character, I can call it NOP.
    NOP
\$\endgroup\$
2
\$\begingroup\$

h, 20 bytes

8,2,9,4,9,6,8,2,-1,0

Neither the language (by Nerdaxe) nor this answer (by VilgotanL) are my creations, but I want to publicize them here.

h (link) is a programming language created by Nerdaxe which is quite limited.

Here's an explanation of how the program works through pseudocode similar to subleq assembler:

neg1 2 #acc is now -1
zero 4 #acc is now 1, the variable zero is 1
zero 6 #acc is now 0, the variable zero is 0
neg1 2 #acc is now -1, go back to 2
neg1:-1 zero:0 #variables
\$\endgroup\$
2
\$\begingroup\$

Daoyu, 1 byte

><

Each instruction is one hex, so two instructions fit into one byte (0x69). The < increases the operating level (a number that affects certain commands) and moves the program reader to the beginning of the program, but only if the op level is less than 9. So, it's preceded by the > command, which decrements the op level (to a minimum of zero).

\$\endgroup\$
1
6 7
8
9 10
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.