1.6 Break, question about how 'True' works in loop


#1

How come the condition here is just 'true'? How does the while loop know what it should be checking is true?

count = 0

while True:
    print count
    count += 1
    if count >= 5:
        break

#2

Well a while loop always expects a boolean to follow. Think about it if you enter a condition there e.g. count > 5. What it does is to evaluate the statement


count = 0
count < 5
--> 0 < 5
--> True


count =6
count < 5
--> 6 < 5
--> False

So if you use an expression you determine in each loop whether it is True or False and act accordingly and with just True you do what you do on True, always -> you run the code -> and run the code -> and run the code -> forever. So it is a really bad idea to do so if you don't have a break inside the loop.


#3

So True here isn't referring to count or anything, it is just a boolean filler to keep the loop going in order to demonstrate how to use break?


#4

Yes that is what it does here. True is just a value so it can never refer to anything anyway.