Python 3, 29 bytes
or~print(0)
while 1:print(1)
A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.
The right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).
If the prefix is 0 the argument (print(0)) of bitwise-not, ~ is evaluated - this has a side-effect of printing 0 and returns None, which is an invalid argument for ~ causing an error which halts the program.
If the prefix is 0 the code ~print(0) is not evaluated (since 1 or x is always True) and the infinite loop of while 1:print(1) is reached.
- 115.4k
- 8
- 68
- 293