Condition:


#1



In the past chapter we learned how to use break to stop a loop from executing.
I've checked and the result is the same. So is there a difference between this two scenarios below ultimately?


loop_condition = True

while loop_condition:
    print "I am a loop"
    loop_condition = False


loop_condition = True

while loop_condition:
    print "I am a loop"
    break


#2

The break statement, like in C, breaks out of the smallest enclosing for or while loop.

where as in the first scenario, the loop gets to the condition a second time and is evaluate to false. It doesn't break free, it evaluates the condition


#3

so one is a definitive break, the other is an evaluation which could lead to a break?


#4

the evaluation leads to False, which simply means the loops job is done and the loop ends. I wouldn't define that as "breaking" the loop.


#5

whats the advantage of writing

instead of "break" in this scenario then?


#6

that you update the loop_condition.

You can use both approaches.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.