+ 1
why do we use while True:
3 ответов
+ 2
while True: is like while (1) of C. it is capital T in True
+ 1
Its for infinite loop.
For example if you want to run a block of code forever you may want to use this technique
For example
while True:
name=str(input("enter your name:" ))
print("hello "+name)
if name == 'quit':
break
now the above code would run the while loop forever since its always True (while loops breaks when condition is false) . Inorder to break it we can use 'break' keyword with a 'if' condition.
if the input you give is equal to 'quit'
the loop breaks
+ 1
these kinds of while loops are used for infinite times iterating,break statement will require inside the loop (within a if block)to break the loop.