English subtitles

← 05-51 Comparing Languages Solution

dummy description

Get Embed Code
3 Languages

Showing Revision 1 created 05/13/2012 by Amara Bot.

  1. Let's go through the answer together.
  2. When I run this program, we start out with x being 0,
  3. and then this while loop says as long as True is true, keep incrementing x.
  4. So now x is 1, it's 2, it's 3, it's 4, it's 5,
  5. and we never actually get to this line about the print.
  6. So does this program print 0? Not so much.
  7. Control flow goes here, here, here, then back,
  8. and we stay in this while loop forever.
  9. It doesn't print out this huge number 4,294,967,296,
  10. which is, I think, pretty close to, let's say, 2 to the 32.
  11. This is a reasonable guess if you think,
  12. "Oh, the integer is going to max out at some value based on computer hardware." But no.
  13. There's a thought that it raises an exception.
  14. Some badly made Python interpreters might raise an exception on this, but it should not.
  15. If we had lots of computing resources, this would run forever.
  16. And similarly, it also never prints out -1.
  17. The correct answer is none of these things. It loops forever.
  18. All right. So if this program naturally loops forever and we're writing an interpreter
  19. that exactly simulates this program by following all of its steps,
  20. our interpreter is also going to loop forever.
  21. If someone had written this program in JavaScript
  22. and put it in the middle of a web page, our web browser would loop forever
  23. and never actually render the resulting web page.