pythonloops

Python forever while loop


I have just started programming with python and I've come across this syntax:

while True:
  do smth

and I can't seem to find a way to stop the loop

I haven't found anything that stops the loop to enable the rest of the program to function and I'd like to find something that does so


Solution

  • As you said it is a forever loop and only stops when the while condition is False.

    so this statement will never stops until you press Ctrl^C and Keyboard Interruption.

    you can break a loop by break keyword inside a loop.

    for example:

    counter = 0
    while True:
        if counter == 10:
            break
        print(counter)
        counter = counter + 1
    

    This simple program has a while loop that prints from 0 to 9 and when counter == 10 then will break the loop.