140
\$\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\$
13
  • 50
    \$\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

553 Answers 553

1
4 5
6
7 8
19
3
\$\begingroup\$

Factor, 10 bytes

[ t ] loop

Try it online!

Factor is not necessarily verbose :P

Given that we need a quotation to repeat and a looping word, I believe this is the very shortest we can get in Factor.

\$\endgroup\$
3
\$\begingroup\$

Scratch, 23 bytes

A "forever" block still runs when it's empty.

when gf clicked
forever
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf! \$\endgroup\$
    – Makonede
    Apr 14, 2021 at 20:16
3
\$\begingroup\$

Arduino, 27 bytes

void setup(){}void loop(){}

This is also the shortest Arduino code that will actually compile. I'm surprised no-one has done this yet.

\$\endgroup\$
3
\$\begingroup\$

Taxi, 22 20 bytes

[a]Switch to plan a.

Try it online!

Not very exciting. TIO will only run it for 60 seconds before timing out.

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

RISC-V machine code, 2 bytes

For CPUs implementing RV32I/E-C or RV64I-C:

01 a0 — c.j .

For RV32I/E-C CPUs only:

01 20 — c.jal .

RISC-V machine code (no extensions), 4 bytes

There are 4 sets of 32[1] instructions that work for this

6f 00 00 00 - j .
ef 00 00 00 - jal .
6f 01 00 00 - jal x2, .
...
ef 0f 00 00 - jal x31, .

The other 96 options are based on the conditional jump instructions beq, bge and bgeu with the same register as operands

[1]: 16 on RV32E

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$ Oct 29, 2021 at 19:19
2
\$\begingroup\$

Spin, 13 Bytes

File x.spin:

pub x
 repeat

(without trailing newline)

Compile and download it to your P8x32a microcontroller or run it using spinsim.

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

AutoIt3, 12 bytes

While 1
WEnd

Simply loop indefinitely. Nothing much to say.

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

BBC Basic for Windows, 3 or 6

http://www.bbcbasic.co.uk/bbcwin/bbcwin.html

RUN

3 ASCII characters. Note in this version of BBC basic line numbers are not required.

If you don't consider termination and self-execution a loop, then the shortest program is

1GOTO1

6 ASCII characters.

One might expect these to be shorter in the tokenized version (1 byte per keyword), but it seems they are not, due to the way line numbers / internal ID's are stored.

\$\endgroup\$
1
  • \$\begingroup\$ Why not 1G.1? \$\endgroup\$
    – Neil
    Oct 3, 2015 at 20:44
2
\$\begingroup\$

GNU sed, 3 bytes

:;b

Using this meta answer as justification for the relaxation of the no-input rule.

: defines a (nameless) label, ; is a line/command separator, b jumps to the label.

Nameless labels seems to be a GNU extension.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Nameless labels seems to be a bug. \$\endgroup\$
    – Ben
    Oct 2, 2015 at 20:33
  • 3
    \$\begingroup\$ @Ben From your link "Not really a bug as such" - I prefer "undocumented feature" ;-) Fair game for codegolf, IMO... Don't use it in production code though. \$\endgroup\$ Oct 2, 2015 at 20:42
2
\$\begingroup\$

Math++, 3 bytes

1>$

Basically a GOTO 1 statement- on line 1.

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

R, 9 8 bytes

repeat 1

Saved 1 byte thanks to MickyT!

\$\endgroup\$
2
  • \$\begingroup\$ You can use repeat 1 for 1 byte saving. \$\endgroup\$
    – MickyT
    Oct 2, 2015 at 20:24
  • \$\begingroup\$ @MickyT Nice, I always forget about repeat. Thanks! \$\endgroup\$
    – Alex A.
    Oct 2, 2015 at 20:26
2
\$\begingroup\$

Ada, 49 47 bytes

procedure L is begin loop null;end loop;end L;

I'm guessing there aren't many Ada entries on Code Golf!

Naturally it should look like

procedure L is 
begin 
   loop 
      null; 
   end loop; 
end L;

and do exactly what it says on the tin.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can you remove the spaces after the semicolons? \$\endgroup\$ Oct 2, 2015 at 20:30
  • \$\begingroup\$ Yes ... 47 bytes. Thanks! (even though it's making me grind my teeth to look at :-) \$\endgroup\$ Oct 2, 2015 at 21:01
2
\$\begingroup\$

Self-modifying Brainfuck, 3 bytes

Same effect as in regular BF. Increment the cell and loop forever. -[] is the same.

+[]

Since the source code in placed on the tape, this is also acceptable, and only works in SMBF:

<[]

If the tape were actually infinite, [ or ] would work, since the interpreter would search for the matching bracket forever. Unfortunately (fortunately?), you just get an "index out of bounds" error.

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

Snowman 1.0.2, 6 chars

~:1;bD

~ sets all the variables to active, :...;bD is a "do" loop (i.e. continues looping while the block returns a truthy value), and 1 is 1.

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

AutoIt, 10 bytes

Do
Until 0

Lame.

\$\endgroup\$
1
  • \$\begingroup\$ AutoIt was my first programming language and this solution is good as well... So Plus One! :) \$\endgroup\$
    – Arjun
    May 12, 2016 at 10:34
2
\$\begingroup\$

Julia, 13 12 bytes

while 1<2end

Yawn. I tried a map and a for loop, but to no avail...

EDIT: Someone pointed out that I could shorten the program by removing parentheses. Thanks!

Alternatively, if stack-overflowing infinite recursion counts as infinite (11 bytes):

i()=i();i()
\$\endgroup\$
2
  • \$\begingroup\$ You don't need the parentheses around 1<2 (in fact it's rather "un-Julian" to have them there). \$\endgroup\$
    – Alex A.
    Oct 2, 2015 at 21:24
  • \$\begingroup\$ Why isn't 1 True, or why isn't 1<2 1? (That is, why doesn't Juila implement bool as a "subclass" of int, like Python does, which is endlessly useful?) \$\endgroup\$
    – cat
    Mar 19, 2016 at 18:38
2
\$\begingroup\$

ferNANDo, 7 bytes

1 1
1
1

Line 1 initializes 1 to 1, line 2 marks the beginning of the loop, and line 3 marks the end (a single variable statement loops back to the previous occurrence, if any, for as long as the variable is true).

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

Objective-C, 1716

-(id)a{for(;;);}
\$\endgroup\$
2
\$\begingroup\$

StackStream, 21 bytes

{ dup exec } dup exec

Kind-of explanation thingy:

{ dup exec } # Push this piece of code onto the data stack.
dup # Duplicate it (stack: { dup exec } { dup exec })
exec # Execute it (stack: { dup exec })
\$\endgroup\$
1
  • \$\begingroup\$ Oh, someone else made a badly-parsed stack-based language? Yay! \$\endgroup\$ Mar 20, 2016 at 2:18
2
\$\begingroup\$

Pascal, 32 bytes

program l;begin while 1=1do;end.

A full program in just one line.

  • program l;: Start a new Pascal program
  • begin: Begin a code section
  • while 1=1do;: Do nothing (; / NOP) as long as 1=1, which shorter than true. Identifiers (do for instance) are not allowed to begin with a digit, so we can omit the space here and save one byte.
  • end.: End the code section
\$\endgroup\$
2
\$\begingroup\$

gs2, 2 bytes

CP437: ►3

Hex dump: 10 33

Tries to split whatever is on STDIN into chunks of length 0. We never actually split off any chunks, not even from an empty string, so this takes forever.

Note that gs2 doesn't have any traditional looping constructs.

(Mitch Schwartz found this and told me about it, and I thought it was a really cute feature.)

\$\endgroup\$
1
  • \$\begingroup\$ What GS2 version is this? It immediately stops on the current version for me \$\endgroup\$ Sep 22, 2016 at 0:41
2
\$\begingroup\$

Whitespace, 9 bytes

;
...; create label [space]
;
.;
.    goto label [space]

. represents a space and ; represents a newline character.

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

RoboTalk, 5 bytes

0 rti

RoboTalk has three "goto" instructions: jump, return, and rti. Jump and return are plain "go to the address on the top of the stack" instructions, while rti has the side effect of (re-)enabling interrupts, as it's intended to be used to exit interrupt handlers. In a robot without any interrupt handlers defined, however, it is functionally equivalent to the other two instructions while being one byte shorter than jump.

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

FlogScript, 2 bytes

F<

The F< (flow-control-restart) command repeatedly restarts the program.

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

Groovy, 8 bytes

for(;;);

Shortened from the original thanks to suggestions. The trailing semicolon is still required, unfortunately.

while(1){}
\$\endgroup\$
2
  • \$\begingroup\$ Can you not just do while(1); do you have to use {} ? \$\endgroup\$ Oct 3, 2015 at 6:04
  • \$\begingroup\$ for(;;); <--what about that, I'm not familiar with Groovy \$\endgroup\$ Oct 3, 2015 at 6:04
2
\$\begingroup\$

Factor, 14 bytes

: a ( -- ) a ;

and nicely symmetrical looking (almost)

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

8085 Intel Microprocessor ,4 1 byte

0000h: pchl

whose machine code is:

E9

Just reset the microprocessor. And load E9 at 0000h.

p.s: its almost impossible in some kit to load program at ROM memory(which is basically the start memory) So, it works on simulator and some special kits only.

C000H: lxi h,C000h
C003H: pchl

whose machine code will be

21 00 0C E9 

loaded in memory from C000H

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

Pyth, 3 bytes

W1 

Note that there is a space after W1.

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

OCaml, 17 bytes

while 1<2do()done

1 byte removed thanks to @Mauris.

Another one bytes the dust thanks to @feersum.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Try 1<2 instead of true. \$\endgroup\$
    – Lynn
    Oct 11, 2015 at 9:06
  • \$\begingroup\$ @Mauris Nice catch, thanks \$\endgroup\$
    – coredump
    Oct 11, 2015 at 9:11
  • 1
    \$\begingroup\$ You can drop the space after 2. \$\endgroup\$
    – feersum
    Oct 15, 2015 at 19:40
2
\$\begingroup\$

Burlesque, 5 bytes

0bc<-

Trying to reverse an infinite list is guaranteed to loop forever and since it loops forever no output will be produced ever.

\$\endgroup\$
1
4 5
6
7 8
19

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.