# Shortest auto-destructive loop

Your task is to write a full program or function that takes no input and runs any type of loop (while, for, foreach, do, do-while, do-loop, goto, recursion, etc) that will end in causing an error, which means that the program must stop itself running and exit.

Rules:

1. The error must be a run-time error, unhandled exception, or anything that makes the program end itself.
2. The error must produce the stop and exit from the program without calling explicitly exit; (or equivalent) at some point.
3. Messages like Warning:, Notice:, etc, that do not cause the program to end itself are not valid. For example in PHP divisions by zero produces a Warning message but the program will not stop and will still run, this is not a valid answer.
4. The loop must run at least one full cycle. In other words the error can happen starting at the second cycle and further. This is to avoid to cause the error using incorrect code syntax: the code must be syntactically correct.
5. The loop can be even infinite (example for(;;);) if it respects the above said rules, but must take no longer than 2 minutes to end itself in a run-time error.
6. Recursion without Tail Call Optimization is invalid (1,2).
7. This is so the shortest code wins.
8. Standard loopholes are forbidden.

C# example (test online):

using System;
public class Program {
public static void Main() {
int i;
int[] n;
n = new int[5];
for(i=0; i<7; i++) {
n[i] = i;
Console.WriteLine(n[i]);
}
}
}

Output:

0
1
2
3
4
Run-time exception (line 9): Index was outside the bounds of the array.

Stack Trace:

[System.IndexOutOfRangeException: Index was outside the bounds of the array.]
at Program.Main(): line 9

Thanks to Martin Ender for the Leaderboard Snippet

• Just to be clear, recursion without TCO can be used as long as the error does not have to do with too much recursion, correct? (For example, a recursive function that errors on the second recursion) – ETHproductions Dec 23 '16 at 22:06
• @ETHproductions It was suggested by Dennis in chat: "It might be difficult to decide if a full cycle has completed in this case [of recursion]. Tail recursion kinda fits the bill, but only TCO does actually complete a cycle if execution is aborted by an error. [...] I'd say recursion without TCO is invalid." – Mario Dec 23 '16 at 22:13
• In for(a;b;c)d;, after wich statement ends the first cycle ? Is it valid to break on the first evalution of c statement ? – Hedi Dec 23 '16 at 23:54
• @Hedi Here's my humble opinion (not the OP): All entries must complete one full cycle, meaning they must enter a second cycle; this means that at least one statement is run a second time. Since the order of execution in your example is a, b, d, c, b, d, c, ..., b is the start of the cycle, and must be run at least a second time. – ETHproductions Dec 24 '16 at 1:20
• I don't want to start any trouble but since the program (of function for that matter) is not supposed to be taking any input, all recursive solutions that have a parameter are invalid because a parameter is input. – BrainStone Dec 25 '16 at 11:43

# Python 3, 35 Bytes

x=lambda a:1/0if a<1 else x(0);x(2)

I am not totally sure, but I think this works like you'd expect. Does that count as an true answer?

for i in [1]:
print i

python code, not sure if this counts because it is ending not erring

for(var i in millis()){for(var j in millis()){for(var k in millis())for(var l in millis())println(i*j*k*l)}}}}

much longer

JavaScript code, will give a runtime error because it is running a ton of code each frame

• I'm pretty sure this is invalid because of the rule: The error must be a run-time error, unhandled exception, or anything that make the program end itself. – DJMcMayhem Dec 26 '16 at 23:00

# QC 3 bytes

\$01

The program jumps into code at address 1, where it encounters 0, which expects 2 bytes after it, not 1. Because of that, it crashes with unexpected end of code message.

To try this code you need compile the interpreter yourself

# Bean, 4 bytes (non-competing)

5¥:(

Assembles to the JavaScript:

while(1);

Try the demo here! Running in a modern browser, this typically prompts the user within 30 seconds with a message saying

The following page(s) have become unresponsive. You can wait for them to become responsive or kill them.

• That costs ¥5? :( – Esolanging Fruit Mar 16 '17 at 6:25

# Javscript (ES6), 12 16 bytes

(a=i=>a())() // Old code, not valid

(a=i=>i?a():z)

This produces the error: Uncaught ReferenceError: z is not defined

• "Recursion without Tail Call Optimization is invalid" – ASCII-only Jun 1 '18 at 10:59
• Sorry, forgot about that. – 77Tigers Jun 1 '18 at 11:24

# k, 4 chars

,:\0

Will terminate after a few seconds with SEGV

# Python 3, 33 16 Bytes

New code:

for i in 1,0:0/i

I realises just after posting this that a division by 1 then 0 would produce an acceptable answer, allowing me to shrink my code by more than 50%

Old code:

for i in 0,1:
if i:raise IOError
• How is this any different to my answer?? – FlipTack Dec 24 '16 at 0:32
• @Flp.Tkc I hadn't realised that someone had already posted this answer. But yes, we have taken the same approach to this. – sonrad10 Dec 24 '16 at 0:34
• Seeing that you've already realized that the aproach is the same, you should either reverte the answer or delete it. – Ismael Miguel Dec 24 '16 at 4:43
• @Flp.Tkc Duplicate answers are allowed – Martin Ender Dec 24 '16 at 9:36
• @MartinEnder Allowed or not, the downvotes will still happen. This answer is just a copy of the other, in it's majority. Nothing new here, nothing to upvote here. Might as well revert to the orgininally original answer and accept the longer byte count. – Ismael Miguel Dec 24 '16 at 17:27