A while loop runs while a condition is true. True is always true, so it will run until something causes it to break (e.g. a break statement, program crash, etc.) For comparison, the first loop in the code you posted could be rewritten without an infinite loop as: age = input("Enter your age: ") while not age.isdecimal(): age = input("Please enter a number for your age: ") Answer from mopslik on reddit.com
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - The loop runs while the condition remains true. When the condition turns false, the loop terminates, and the program execution proceeds to the first statement after the loop body.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-while-loop-tutorial
Python While Loop Tutorial โ€“ While True Syntax Examples and Infinite Loops
November 13, 2020 - When we write a while loop, we ... the process and False to stop it. ๐Ÿ’ก Tip: if the while loop condition never evaluates to False, then we will have an infinite loop, which is a loop that never stops (in theory) without ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ can someone explain what this `while true` function is actually checking?
r/learnpython on Reddit: Can someone explain what this `while True` function is actually checking?
December 13, 2023 -

https://pastebin.com/NWkKQc1P

It's from Automate the Boring Stuff. I tried running it through the python tutor and it didn't clarify. Is the basic point that there actually isn't anything it's checking to be True, so it's an infinite loop until you trigger the `break`?

PS I figure this is something simple from much earlier that I didn't internalize. I plan to go through all the basic curriculum again to make sure there aren't any gaps in super basic knowledge.

Top answer
1 of 8
23
A while loop runs while a condition is true. True is always true, so it will run until something causes it to break (e.g. a break statement, program crash, etc.) For comparison, the first loop in the code you posted could be rewritten without an infinite loop as: age = input("Enter your age: ") while not age.isdecimal(): age = input("Please enter a number for your age: ")
2 of 8
9
Correct; this is just a way to start an infinite loop that you intend to break with some condition/break statement later on. Mostly used for starting/running an application that you don't want to close down without user intent (clicking an exit/quit button in a UI or entering "quit", "exit", etc from the command line. It *isn't* a great construct to use when you want something that has solidly defined criteria to repeat until those criteria are met. As mopslik pointed out, you would be better served in those examples to actually use the defined criteria in those as your loop conditions since you're not at risk of just sticking the user in an infinite loop if you forget to write your break statement somewhere. You, also, don't have to write so many lines to accomplish the same goal. ``` password = "" while not password.isalnum(): password = input('Select a new password (alphanumeric only') print("Password is good") ``` You do not have to print a line and then call input(), either. Just put the message you want displayed into the parens for the function call: input("Enter your age: ")
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-use-while-true-in-python
How to use while True in Python - GeeksforGeeks
July 23, 2025 - In Python, loops allow you to repeat code blocks. The while loop runs as long as a given condition is true. Using while True creates an infinite loop that runs endlessly until stopped by a break statement or an external interruption.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Python while Loop (Infinite Loop, break, continue) | note.nkmk.me
August 18, 2023 - If the condition in the while statement is always True, the loop will never end, and execution will repeat infinitely. In the following example, the Unix time is acquired using time.time(). The elapsed time is then measured and used to set the ...
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ use-while-true-in-python
Use While True in Python | Board Infinity
August 13, 2025 - It is often used with a break statement to exit the loop under certain conditions. "While True" loops should be used cautiously, as they can run indefinitely if the break statement is not properly implemented. They are commonly used to create ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
With the break statement we can stop the loop even if the while condition is true: ... Note: The else block will NOT be executed if the loop is stopped by a break statement. ... If you want to use W3Schools services as an educational institution, ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ loops-in-python
Loops in Python - GeeksforGeeks
Code given below uses a 'while' loop with the condition "True", which means that the loop will run infinitely until we break out of it using "break" keyword or some other logic. ... Note: It is suggested not to use this type of loop as it is ...
Published ย  June 7, 2017
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
Another easy infinite loop bug is forgetting the i = i + 1 line entirely, so the variable never advances and the loop never exits. Since the more commonly used for-loop automates the increment step for us, we don't quite have the muscle memory to remember it when writing a while-loop. Suppose there is some function foo() and you want a while loop to run so long as it returns True...
๐ŸŒ
The Knowledge Academy
theknowledgeacademy.com โ€บ blog โ€บ python-while-loop
Python While Loop: Everything You Should Know
... If you forget to update the loop control variable inside the While Loop, the condition always stays true. This leads to an infinite loop and your program never exits on its own.
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ python โ€บ python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
Instead of declaring any variable, applying conditions, and then incrementing them, write true inside the conditional brackets. ... The following code will run infinitely because "True" is always "True" (no pun intended!).
๐ŸŒ
Gitbooks
buzzcoder.gitbooks.io โ€บ codecraft-python โ€บ content โ€บ while-loop โ€บ infinite-loop-and-break.html
Infinite loops and break ยท CodeCraft-Python
This can be done with break keyword. ... Here is a good example of an infinite loop that works: while True: n = int(input('Give me an integer: ')) if n == 0: break print(str(n) + '*' + str(n) + '=' + str(n*n)) print('done')...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ while-loops-in-python-while-true-loop-statement-example
While Loops in Python โ€“ While True Loop Statement Example
July 19, 2022 - To stop this from being an infinite loop, I first introduce an if statement. The if statement checks whether i is equal to 5. If it is, then the loop will come to an end thanks to the break statement inside the if statement, which essentially tells the loop to stop. i = 0 while True: print(i) i = i + 1 if i == 5: break ยท And there you have it! You now know how to write while and while True loops in Python.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ py4e-int โ€บ iterations โ€บ infinite_loops.html
6.3. Infinite loops โ€” Python for Everybody - Interactive
while True: line = input('Word: ') if line == 'done': break print(line) print ('Done!') The loop condition is True, which is always true, so the loop runs repeatedly until it hits the break statement.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ looping-technique
Python Looping Techniques
# An example of infinite loop # press Ctrl + c to exit from the loop while True: num = int(input("Enter an integer: ")) print("The double of",num,"is",2 * num)
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ sorry to ask such a silly question but what does 'while true:' actually mean?
r/learnpython on Reddit: sorry to ask such a silly question but what does 'while True:' actually mean?
November 14, 2023 -

what does the True refer to?

eg if i write:

while True: 
    print("hi")

i know that "hi" will be repeated infinitely, but what is it thats True thats making "hi" get printed?

also, whatever it is thats True, im assuming its always true, so is that why if i typed 'while False', none of the code below would ever run?

sorry if this is a stupid question

edit: also, besides adding an if statement, is there anything else that would break this infinite while loop?

and how would i break the loop, lets say after "hi" has been printed 10 times, using an if statement or whatever else there is that can break it. thanks!

๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ python while loop
Python While Loop: Explained with While Loop Flowchart
October 14, 2025 - Yes, a while loop can run infinitely when the condition mentioned in the loop structure will always be true. In order to avoid these infinite loops, you can use break statements. 3. How does a while loop differ from a for loop in Python?