
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to prevent loops going into infinite mode in Python?
Loops formed with for statement in Python traverse one item at a time in a collection. Hence there is less likelihood of for loop becoming infinite.
The while loop however needs to be controlled by making some provision inside the body of the loop to drive the condition mentioned in the beginning to false.This is usually done by keeping count of iterations
x=0 while x<5: x=x+1 print (x)
Loop repetition can also be controlled by using break to take an early exit from the iterations
while True: stmt1 stmt2 if expr==True: break stmt3 ..
- Related Articles
- How do I prevent an iOS device from going to sleep mode?
- How do I prevent an Android device from going to sleep mode?
- How to handle Infinite loops in Postman Workflow?
- How to prevent going back to the previous activity in Android?
- Index into an Infinite String in Python
- How to use nested loops in Python?
- How to use else statement with Loops in Python?
- How to make loops run faster using Python?
- How to stop an infinite loop safely in Python?
- How to use single statement suite with Loops in Python?
- In ABAP, How to select all the data into my internal table using loops?
- How to use multiple for and while loops together in Python?
- How to iterate over dictionaries using 'for' loops in Python?
- How do I run two python loops concurrently?
- How can I represent an infinite number in Python?

Advertisements