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"


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


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


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.


whats the advantage of writing

instead of "break" in this scenario then?


that you update the loop_condition.

You can use both approaches.


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