Fake infinite loop

1

The challenge is to write a fake infinite loop in any imperative programming language.

At first sight, the code should seem not to halt at all.

As a naive example:

int f(int x)
{
    return f(4.0);
}

int f(double x)
{
    return 0;
}

But remember
You're not allowed to use overflows:

int f(int x)
{
    if(x < 0) return -1;
    return f(x*x);
}

is illegal. When the number exceeds the integer limit, the function will stop. It's a good trick. But not good enough :)

The answer with the highest votes wins! Duration is 1 week.

padawan

Posted 2014-07-07T21:05:58.000

Reputation: 294

Question was closed 2014-07-07T22:15:04.230

@m.buettner You're right. I changed it to popularity contest. Anything else is good enough. Overflows is the first thing that pops into one's mind (as well as mine). – padawan – 2014-07-07T21:29:19.640

I'd suggest maybe broadening the anti-overflow rule to just say you can't break out of the loop using an error/exception. – histocrat – 2014-07-07T21:31:10.797

1

Nevertheless, I don't think this adds a lot over this challenge because I think in most cases you will be able to find some kind of termination condition that you just need to invert to fulfil the condition for this challenge. I'd vote to close as duplicate (instead of my now wrong close reason "unclear"), but I can't close vote again if I retract, so I guess the wrong reason stays for now.

– Martin Ender – 2014-07-07T21:35:25.763

3It's unclear too. Neither of the two examples looks remotely like an infinite loop. – Peter Taylor – 2014-07-07T21:38:49.190

For the record, I voted to close as duplicate rather than as unclear, so I don't see any point in trying to improve the clarity, but since I see that someone has voted to reopen: the second example is still unclear IMO. What if I call f(0)? (And the first example still wouldn't fool anyone with an IQ above 90 and at least two hours of programming experience). – Peter Taylor – 2014-07-08T08:23:26.393

Answers

5

Bash

trap 'exec "$0"' exit

It re-execs itself when it should exit. So it will never really exit.

It doesn't have the shebang. So you must run it like bash a.sh. And $0 will probably not be something executable.

jimmy23013

Posted 2014-07-07T21:05:58.000

Reputation: 34 042