Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

2 of 4
added 249 characters in body
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

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.

Try 0 online!
Try 1 online!

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.

Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

AltStyle によって変換されたページ (->オリジナル) /