-1
while 1:1

What is the output of the above code? Is there some way to check that the above loop is doing something or nothing? One way is to enter a print statement in the body of the while loop like:

while 1:
  1
  print "2"

But, without entering anything new in the body of the while loop can we check that the loop is at least doing something?

Coding man
  • 957
  • 1
  • 18
  • 44
  • 1
    What do you mean by it "doing something"? As long as the program doesn't exit, it 's rather obvious that it's doing *something*: looping. What other kind of action is it that you're looking for? – Dolda2000 Jan 29 '14 at 05:15
  • Perhaps you can look at the task manager and see the resources going to the process? Obviously if the only code you are allowing to be written is `while 1:1` you're going to need to look somewhere other than the IDLE Gui or python output. – chase Jan 29 '14 at 05:18

3 Answers3

5

You can check that in this way:

>>> def f():
...   while True: 42
... 
>>> 
>>> import dis
>>> dis.dis(f)
  2           0 SETUP_LOOP              10 (to 13)
        >>    3 LOAD_GLOBAL              0 (True)
              6 POP_JUMP_IF_FALSE       12
              9 JUMP_ABSOLUTE            3
        >>   12 POP_BLOCK           
        >>   13 LOAD_CONST               0 (None)
             16 RETURN_VALUE        
>>> 

So you can see, it just does nothing besides the looping.

I am surprised, to be honest, that it doesn't even load the given value somewhere.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • I suppose that loading an immutable global and then not storing it anywhere is an easy thing to optimize out... – mgilson Jan 29 '14 at 05:19
  • That's interesting, especially after reading this [question](http://stackoverflow.com/questions/21260745/what-happens-if-you-write-a-variable-name-alone-in-python). With variable names, they're pushed onto the stack and immediately popped off. – jayelm Jan 29 '14 at 05:19
  • What will you say about the output of each iteration of the loop? Is it "nothing" ? – Coding man Jan 29 '14 at 05:25
  • 1
    @Codingman If I do a `print`, it will print. But otherwise, unless I am at the top level of an interactive session, it indeed won't. – glglgl Jan 29 '14 at 05:26
  • @glglgl -- I suppose if you look at `dis.dis` output enough, you find that it does optimize away *some* things, just not that much :). – mgilson Jan 29 '14 at 05:28
  • @glglgl -- It gets even more interesting if you do `while 1:1`. Then even the `LOAD_GLOBAL (True)` goes away. It might be that way if you do it on python3 too where `True` becomes a keyword... – mgilson Jan 29 '14 at 05:30
  • The `LOAD_CONST` changing to a `None` is a bit of compile-time magic/optimization. I'm tempted to lump it in with constant folding but I think "dead code elimination" is considered a separate thing. One of the two, I'm not digging through `compile.c` to check :-) – roippi Jan 29 '14 at 05:39
2

You seem to be laboring under some kind of misconception. I don't know if I'm interpreting you correctly, but under normal circumstances, a program you write doesn't even have the option of not "doing something": It is a fundamental aspect of a computer that it is constantly executing instructions.

The only way you could truly make a computer "not do anything" would be if you're programming at the hardware level, where you could tell the CPU to halt, but on normal architectures, only the kernel has access to such privileged instructions, and high-level programming languages like Python do not expose them.

What you could do in order to not do anything, at the level of a user program, is to ask the kernel to not schedule your process, such as by executing a call to sleep, or a blocking I/O call, or the like. Since you're not doing any such thing explicitly, however, there is no alternative for your program other than to execute. Asking to "verify" whether it's doing something is like asking to verify that 1 + 1 = 2: It's simply true by definition.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
1

The loop is most certainly doing something, though arguably nothing useful. If you're in the interactive shell, the output is an endless stream of 1s. In a script, there will be no output.

In python, 1 evaluates to True, so the while loop is similar to while True:

Try this:

print "While loop starting!"
while 1:1
print "While loop has finished!"

And you'll discover the second line never prints.

jayelm
  • 7,236
  • 5
  • 43
  • 61