147
\$\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>

LegionMammal978
18.2k4 gold badges25 silver badges58 bronze badges
asked Oct 2, 2015 at 17:20
\$\endgroup\$
14
  • 54
    \$\begingroup\$ I've got to start posting programs with a negative byte count to beat all these empty files! \$\endgroup\$ 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\$ 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\$ Commented Oct 6, 2015 at 16:47
  • \$\begingroup\$ Can you print "", an empty string? \$\endgroup\$ Commented Mar 7, 2016 at 23:39

588 Answers 588

1
...
7 8
9
10 11
...
20
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!

answered Nov 3, 2020 at 4:21
\$\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.

answered Nov 23, 2020 at 20:01
\$\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\$ Commented Nov 23, 2020 at 20:13
2
\$\begingroup\$

Python 3, 14 bytes

{*iter(int,1)}

Try it online!

answered Mar 12, 2021 at 5:40
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can just use while 1:pass \$\endgroup\$ Commented Nov 5, 2024 at 7:57
  • \$\begingroup\$ Wow that was right in front of my face the whole time. Great find! For that matter while 1:1 satisfies the prompt as well. Ha looks like Ruth Franklin found this in 2015 codegolf.stackexchange.com/a/59360 \$\endgroup\$ Commented Nov 9, 2024 at 1:05
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.

answered Apr 26, 2021 at 7:36
\$\endgroup\$
3
  • \$\begingroup\$ The least bytes is a 0-byte C program compiled with GCC-EFI. \$\endgroup\$ Commented May 11, 2021 at 20:49
  • \$\begingroup\$ I believe you can replace the {} for the loop body by a single semicolon ; \$\endgroup\$ Commented Dec 25, 2021 at 16:29
  • \$\begingroup\$ @Makonede wait THAT was possible? \$\endgroup\$ Commented 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.
answered May 12, 2021 at 9:22
\$\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

answered May 29, 2021 at 21:35
\$\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\$ Commented Jun 8, 2021 at 21:41
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
answered Jun 22, 2021 at 4:22
\$\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
answered Jun 28, 2021 at 16:20
\$\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).

answered Jun 28, 2021 at 17:32
\$\endgroup\$
2
\$\begingroup\$

Minim, 4 Bytes

C--.

Decrements the program counter, which is incremented back to 0 after the statement finishes.

GitHub Repository

answered Aug 14, 2021 at 0:53
\$\endgroup\$
2
\$\begingroup\$

Braingolf, 4 bytes

[1+]

Try it online!

Braingolf v2, 3 bytes

1[]

Try it online!

answered Jun 6, 2017 at 6:34
\$\endgroup\$
2
\$\begingroup\$

KonamiCode, 12 bytes

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

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

answered Nov 12, 2021 at 15:27
\$\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.

answered Dec 11, 2019 at 16:23
\$\endgroup\$
2
\$\begingroup\$

Lost, 1 byte

?

Try it online!

answered Feb 22, 2022 at 18:57
\$\endgroup\$
2
\$\begingroup\$

Rust, 6 bytes

loop{}

Try it online!

That's one very fast loop

answered May 12, 2022 at 17:59
\$\endgroup\$
2
\$\begingroup\$

BitCycle, 3 bytes

1><

Try it online!

Explanation (because i can)

1 Creates a 1 bit, heading east by default
 > Sets the direction to east
 < Sets the direction to west
 > Sets the direction to east again, creating a loop
answered May 14, 2022 at 17:50
\$\endgroup\$
2
\$\begingroup\$

Knight, 3 bytes

W1N

Epic W1N.

answered Aug 5, 2022 at 2:44
\$\endgroup\$
3
  • \$\begingroup\$ this has already been posted btw \$\endgroup\$ Commented Aug 5, 2022 at 3:04
  • \$\begingroup\$ @Steffan Should I delete it? \$\endgroup\$ Commented Aug 5, 2022 at 3:06
  • \$\begingroup\$ No need to. \$\endgroup\$ Commented Aug 5, 2022 at 3:07
2
\$\begingroup\$

[min]mod, 2 bytes

IS

Treats the instruction stack as an instruction and calls it recursively.

Try it!

Try it step by step!

answered Apr 17, 2023 at 5:37
\$\endgroup\$
2
  • 1
    \$\begingroup\$ No need to mark it as non-competing. :) That isn't a thing anymore since 2017. This challenge also explicitly allows newer languages in the challenge description: "Feel free to use a language (or language version) even if it's newer than this challenge." \$\endgroup\$ Commented Apr 17, 2023 at 11:51
  • \$\begingroup\$ @KevinCruijssen I didn't know that, thanks! \$\endgroup\$ Commented Apr 17, 2023 at 12:51
2
\$\begingroup\$

Tsept v1.0, 5 bytes

Tsept is an esolang I created; it's not good at golfing since even basic tasks like storing numbers and printing strings require a lot of instructions.

This program is 5 bytes long because it has to calculate and repeatedly push the jump offset.

xDDPJ

Note: Due to a bug in the interpreter, the 'J' instruction jumps to the wrong offset (specifically the original offset plus one).

'x' clears the value in the accumulator since, at the start, it contains the interpreter's version.

'D' decrements the interpreter, and due to the aforementioned bug, it has to do it twice.

'P' pushes the accumulator onto the stack.

'J' pops a value from the stack and adds it to the instruction pointer, effectively performing a relative jump.

answered Jun 22, 2023 at 19:16
\$\endgroup\$
2
\$\begingroup\$

CASIO BASIC (CASIO fx-9750GIII), 4 bytes

Lbl A
Goto A
answered Nov 20, 2024 at 16:11
\$\endgroup\$
2
\$\begingroup\$

Regenerate, 3 bytes

1ドル+

Attempt This Online!

Outputs the smallest nonzero number of repetitions of a backreference to the first capture group for which the first capture group has actually captured something. Unfortunately for the poor unsuspecting interpreter, the first capture group doesn't even exist.

answered Dec 7, 2024 at 2:53
\$\endgroup\$
1
\$\begingroup\$

AutoIt3, 12 bytes

While 1
WEnd

Simply loop indefinitely. Nothing much to say.

answered Oct 2, 2015 at 19:21
\$\endgroup\$
1
\$\begingroup\$

Swift, 12 bytes

while(1>0){}
answered Oct 2, 2015 at 20:05
\$\endgroup\$
2
  • 1
    \$\begingroup\$ What about while(1){}? \$\endgroup\$ Commented 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\$ Commented Aug 7, 2019 at 10:56
1
\$\begingroup\$

Rust, 17 chars

fn main(){loop{}}

Nothing much interesting to see here.

answered Oct 2, 2015 at 21:28
\$\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.

{.~}.~
answered Oct 2, 2015 at 21:49
\$\endgroup\$
1
\$\begingroup\$

Lua, 11 bytes

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

::y::goto y
answered Oct 2, 2015 at 22:42
\$\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

answered Oct 2, 2015 at 22:04
\$\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).

answered Oct 2, 2015 at 19:09
\$\endgroup\$
1
  • \$\begingroup\$ CoffeeScript actually has a keyword for infinite loops: loop 1 \$\endgroup\$ Commented 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.

answered Oct 3, 2015 at 0:38
\$\endgroup\$
1
\$\begingroup\$

STATA, 10 bytes

while 1{
}

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

answered Oct 3, 2015 at 5:37
\$\endgroup\$
4
  • \$\begingroup\$ Is the line break needed? I've never used STATA \$\endgroup\$ Commented 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\$ Commented May 17, 2016 at 14:19
  • \$\begingroup\$ What about a: goto a? :D 9 bytes —— stata.com/manuals14/m-2goto.pdf \$\endgroup\$ Commented 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\$ Commented May 17, 2016 at 17:44
1
...
7 8
9
10 11
...
20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.