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

477 Answers 477

1
\$\begingroup\$

Aceto, 1 byte

O
O jumps back to the origin, which conveniently is where the O is!

Try it online!

I didn't see @L3viathan's post so...

Some multi-byte solutions

  • @1$
  • 1§uu
  • 1§##

  • 3§ _
    | |
    _

Try It Online!

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

Swift, 11 bytes

while 0<1{}

Works both compiled and as a shell script.

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – 0 ' Feb 12 '18 at 10:53
  • \$\begingroup\$ Thanks, @0 '! I'm starting to explore CG-possibilities in Swift despite of its annoying "Expression is too complex" :) \$\endgroup\$ – Dan Karbayev Feb 13 '18 at 11:26
1
\$\begingroup\$

Clojure, 9 bytes (old 15 bytes) thank to @Dennins

(while 1)
\$\endgroup\$
  • 1
    \$\begingroup\$ A simple (while 1) would work as well. Try it online! \$\endgroup\$ – Dennis Feb 13 '18 at 2:51
  • \$\begingroup\$ @Dennins, upvoted \$\endgroup\$ – NTCG Feb 13 '18 at 3:08
  • 1
    \$\begingroup\$ That was intended as a golfing suggestion; feel free to edit it into your post. \$\endgroup\$ – Dennis Feb 13 '18 at 3:33
1
\$\begingroup\$

ORK, 132 bytes

There is such a thing as a y.
A y can z.

When a y is to z:
I am to loop.

When this program starts:
I have a y called X.
X is to z.

Try it online!

Defines a class y with a member function z that does nothing but loop. Then instantiates y and calls its z function.

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

VBA 7 Bytes

Do
Loop

I like VBA; it does exactly what you tell it

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

Gol><>, 0 bytes

Try it online!

Just for completeness. This works just like zero-byte Befunge infinite loop.

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

C (gcc), 16 bytes

main(){for(;;);}

Try it online!

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

DUP, 6 bytes

[1][]#                            {infinite while loop}

This is a proper and implementation independent solution. Mama Fun Roll’s 2 byte solution only works with the quirkster Javascript implementation that behaves a bit strangely in some cases.

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

Q'Nial7, 22 bytes

WHILE =1 DO 1 ENDWHILE
\$\endgroup\$
1
\$\begingroup\$

Ahead, 1 byte

l

l turns the head left 45 degrees. Since the board is 1x1, the head will keep trying to move but go nowhere. The head never stops until it encounters a @, so this "loops" forever.

I figured this would be more interesting than the alternative solution, one space character.

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

HadesLang, 15 bytes

while[true]
end
\$\endgroup\$
1
\$\begingroup\$

LOLZ, 9 Characters

lolloolol

lol: creates the loop statement

loolol: returns the value "1"

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

JavaScript (Node.js), 9 bytes

while(1);

Try it online!

\$\endgroup\$
  • \$\begingroup\$ The TIO link shows a syntax error. \$\endgroup\$ – Dennis Apr 14 '18 at 18:22
  • \$\begingroup\$ Weird. I think it broke something in TIO since my code doesn't have any braces. The error is SyntaxError: Unexpected token } <--- my code has no } \$\endgroup\$ – Muhammad Salman Apr 14 '18 at 18:25
  • \$\begingroup\$ Where does this work then? \$\endgroup\$ – Dennis Apr 14 '18 at 18:39
  • 1
    \$\begingroup\$ @MuhammadSalman The reason the code doesn't work is because you are declaring a loop with no contents which is not valid syntax for JavaScript. The semicolon is required so that the loop contains a single empty statement. \$\endgroup\$ – fəˈnɛtɪk Apr 15 '18 at 1:49
  • 1
    \$\begingroup\$ Oh yeah , I forgot. \$\endgroup\$ – Muhammad Salman Apr 15 '18 at 4:05
1
\$\begingroup\$

Piet, 0 bytes

An empty image will make Piet loop around forever...

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

INTERCAL, 16 bytes

(1)DOCOMEFROM(1)

Try it online!

Probably the first time I've managed to get away with not GIVING UP. This program goes to line 1 after line 1 finishes executing. (The shorter (1)DO(1)NEXT very quickly maxes out the NEXT stack and disappears into the black lagoon.)

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

Flobnar, 1 byte

@

Try it online!

@ indicates the entry point of a program and evaluates the cell to its west. There is nothing to the west, meaning it wraps around and evaluates @ again.

The language spec is unclear about whether it is legal for @ to evaluate itself recursively. The linked interpreter supports it. It also does tail-call elimination, meaning that this is really an infinite loop and will not cause a stack overflow.

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

TIS-100, 0/5 bytes

Nonempty solution:

JRO 0

It's really hard to tell the difference between "halting" and "repeating infinitely" in TIS-100, since every program loops automatically. As such, I have done both an empty and nonempty solution.

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

Brian & Chuck, 7 bytes

!{?
!{?

Try it online!

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

Turing Machine But Way Worse, 27 13 bytes

0 0 0 1 0 0 0

Try it online!

Explanation:

Rest of the number isn't necessary, as they are text-based and pointer commands.

0 0 0 1 0 0 0-> do not halt
  |     | |
  |     | +-> do not print
  |     +--> go to state 0
  |
  +---------> if state 0
\$\endgroup\$
  • \$\begingroup\$ Just 0 0 0 1 0 1 0 would work, but that's a duplicate answer of mine \$\endgroup\$ – MilkyWay90 Jun 1 at 13:36
1
\$\begingroup\$

Backshift, 1 byte

0

How it works

This tries to move 0 1-0=1 times backwards. This does not halt, as it tries to do this until it encounters a 1 (which is impossible).

Adar, 7 bytes

[(0,1)]

How it works

This is a looping counter; that means it starts from 0 and counts up in 1s forever.

\$\endgroup\$
  • 2
    \$\begingroup\$ This should be split up into two answers. \$\endgroup\$ – lirtosiast Jul 6 at 9:44
1
\$\begingroup\$

Forth, 19 bytes

: b begin again ; \ compiles word that loops endlessly
b                 \ executes this word

Try it online!

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

jq -n, 9 + 3 = 12 bytes

def f:f;f

The -n option makes jq not consume any input, otherwise it waits for stdin before starting the loop.

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

Pizza Delivery, 2 bytes

(]

While L is zero, end while.

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

Cascade, 1 byte

|

Try it online!

or (if my understanding is correct) any other one-byte program from @/\!^?_$<>=~+-*:%().#--that is, any instruction which depends on the return value of what's below it, either directly or to the left or right, since in the case of a one-byter all three of those wrap back around to the same command. I suspect that most if not all of these would run out of call stack given enough time, but I don't know Perl so I can't quite tell what the interpreter is actually doing.

\$\endgroup\$
  • 1
    \$\begingroup\$ Perl 6 doesn't have a hard recursive limit, so it would just recurse until it runs out of memory \$\endgroup\$ – Jo King Sep 25 at 6:47
1
\$\begingroup\$

1+, 5 bytes

1##1#

Explanation:

1#    [Jump to 1st hash]
  #   [1st hash in program (counting starts from 0)]
   1# [Jump to 1st hash]

Alternatively, 5 bytes

(|())

Explanation:

This program define and execute a function with an empty name. The function calls itself. Thus, it is infinite recursive.

\$\endgroup\$
  • \$\begingroup\$ Whew... Aww, counting starts from 0! I thought it starts from 1. \$\endgroup\$ – HighlyRadioactive Sep 25 at 4:52
1
\$\begingroup\$

Nandy, 4 bytes

:#()

Never decrement: never end. (0 NAND 0 is 1. Thanks to @EdgyNerd for noting that.)

\$\endgroup\$
  • \$\begingroup\$ This can be 4 bytes by just flipping to top of the stack then looping (since 0 NAND 0 is 1) \$\endgroup\$ – EdgyNerd Oct 6 at 14:32
0
\$\begingroup\$

HPPPL, 16 bytes

while 0=0 do end
\$\endgroup\$
0
\$\begingroup\$

C++ Template Meta-Programming, 48 bytes

template<int n>class S{enum{v=S<n-1>::v}}S<1>::v

There is an infinite loop in the template, so this doesn't compile. Other stuff that doesn't compile, but doesn't need to for the template to recurse (S being private, S<1>::v being free floating)

Can be run at http://coliru.stacked-crooked.com/ for error g++: internal compiler error: Segmentation fault

g++ -std=c++14 -O2 -Wall -pedantic -pthread -ftemplate-depth=162345 main.cpp && ./a.out
\$\endgroup\$
  • \$\begingroup\$ Not sure if that is compliant with the rules as it doesn't run out of memory when it stops. If you want to do that, there is the -ftemplate-depth compiler option in gcc that allows you to set the recursion depth to something very high so you'll at least get a segfault out of it. \$\endgroup\$ – iFreilicht Oct 16 '15 at 14:23
  • \$\begingroup\$ @iFreilicht Yeah, I suppose you're right. I couldn't (be bothered to...) find a online complier that supported that. \$\endgroup\$ – Nathan Cooper Oct 16 '15 at 14:33
  • 1
    \$\begingroup\$ coliru.stacked-crooked.com \$\endgroup\$ – iFreilicht Oct 16 '15 at 14:35
0
\$\begingroup\$

Univar, 8 bytes

($,,)$,,

Self-calling function.

\$\endgroup\$
  • \$\begingroup\$ Seems too recursive to me... is there any tail-call optimization? \$\endgroup\$ – Erik the Outgolfer Sep 25 '16 at 13:57
  • \$\begingroup\$ @EriktheGolfer Not at my computer right now, but I believe that the interpreter internally uses a variable-length list for the call stack. \$\endgroup\$ – LegionMammal978 Sep 25 '16 at 14:03
0
\$\begingroup\$

x86 Machine code, 2 bytes

Machine Code

     00 01
0000 73 FE

Asm source

[SECTION .text]
[bits 16]
[org 0x100]

EntryPoint:
    jnc     EntryPoint

Both the jmp and the call instructions use a 1 byte op-code followed by a 2 byte absolute offset. Conditional jumps on the other hand, are limited to a destination within [+127, -128] bytes of the branch. This is because they are encoded with a 1 byte op-code and a 1 byte relative offset. Dos already cleared the flags before invoking our program, so we know the carry flag will be clear and facilitate an endless (non-crashing) loop. We wont run out of stack-space, which would result in a stack-overflow error, or overwriting and ultimately crashing, our program. Approaches using call will suffer this after about 32,638 iterations. (stack is initally 0xFFFE, (3 byte) program begins at 0x100, each iteration decrements the SP by 2.)

\$\endgroup\$

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.