Closing a While Loop in Python Using JavaScript

To terminate a while loop, pressing Ctrl C is an option. This will result in the loop being stopped. To stop the loop, follow this solution: If, at any point in the middle of the loop, becomes greater than, the loop will not break halfway through an iteration. Another way to stop the loop is by performing a Ctrl – C.


Solution 1:

Have you considered incorporating a flag in the loop?

flag = True
while True:
  if flag:
    do function B()
  if condition:
    flag = false


Solution 2:


To halt a while loop, pressing Ctrl C is an option.

while True:
    print("Hello")

To stop it, do ctrl – c

Ctrl C

This is what you will get:

   ^CTraceback (most recent call last):
        File "main.py", line 1, in 
             input()
        KeyboardInterrupt

Frequently Asked Questions