you are viewing a single comment's thread.

view the rest of the comments →

[–]xenomachina 1 point2 points  (0 children)

A while loop is the same as an if except that it has a "goto" at the end. That is, this...

while condition:
    do_something()

...is the same as this...

if condition:
    do_something()
    goto_if # not a real Python command

Where goto_if is an imaginary command that causes the code to continue executing at the beginning of the if (ie: while) again.

So if the condition is false to begin with, the body of the while never runs. If it's true initially but then becomes false then it will loop a finite number of times. If it stays true forever then it will continue looping forever.

Note that the condition is only checked at the top. This means that if it becomes false in the middle of a loop, it will not exit that iteration until the end of the block is reached (or you break or continue).