Consider how the terms are used in common language: FOR For each day in July, I will practice writing loops. For every student in the class, provide a lunchtime meal. For each letter in the alphabet, write down a word beginning with that letter. For numbers in the range 1 to 10, calculate the square root. Notice that for each case, we iterate through multiple things (days in the month / students in the class / letters in the alphabet / numbers in a range). WHILE While there is still daylight, we can play football. While the music is playing, we will dance. While I am waiting, I will read a book. While my set of Pokémon cards is incomplete, I will keep collecting them. Notice that in each case, something is done for as long as a condition (a "predicate") is satisfied (is "True"). In these examples, the predicates are: "There is daylight?", "The music is playing?", "I am waiting?", "The set is incomplete?". As soon as the answer is "False", the loop stops. Answer from JamzTyson on reddit.com
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - In this tutorial, you'll learn about indefinite iteration using the Python while loop. You'll be able to construct basic and complex while loops, interrupt loop execution with break and continue, use the else clause with a while loop, and deal with infinite loops.
Discussions

Whats the difference between a while loop and a for loop?
Consider how the terms are used in common language: FOR For each day in July, I will practice writing loops. For every student in the class, provide a lunchtime meal. For each letter in the alphabet, write down a word beginning with that letter. For numbers in the range 1 to 10, calculate the square root. Notice that for each case, we iterate through multiple things (days in the month / students in the class / letters in the alphabet / numbers in a range). WHILE While there is still daylight, we can play football. While the music is playing, we will dance. While I am waiting, I will read a book. While my set of Pokémon cards is incomplete, I will keep collecting them. Notice that in each case, something is done for as long as a condition (a "predicate") is satisfied (is "True"). In these examples, the predicates are: "There is daylight?", "The music is playing?", "I am waiting?", "The set is incomplete?". As soon as the answer is "False", the loop stops. More on reddit.com
🌐 r/learnpython
69
124
July 15, 2024
python - How to emulate a do-while loop? - Stack Overflow
I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work: list_of_ints = [ 1, 2, 3 ] iterator = list_of_ints.__iter__() element = None More on stackoverflow.com
🌐 stackoverflow.com
Is it weird that I NEVER use while loops? It’s just not something I’ve felt like I needed, ever. What are some situations where using a while loop is necessary?
Simplest example... Asking for user input, validating it, and asking it again if the user got it wrong, in a loop, forever, until he gets it right... For example I want only positive numbers, how would you do that with a for loop? More on reddit.com
🌐 r/learnpython
189
270
September 29, 2022
Need help understanding this while loop
I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number More on discuss.python.org
🌐 discuss.python.org
0
0
February 13, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied.
Published   December 23, 2025
🌐
Substack
adlrocha.substack.com › p › adlrocha-auto-research-the-lab-that
@adlrocha - Auto-research: The Lab that runs while you sleep
3 days ago - Karpathy’s choice of that metric, rather than something noisier, slower, or more expensive, is a big part of why the system works at all. Even more, the fact that the runs are limited to 5 minutes provides a fast feedback loop to the agents that allows them to iterate fast and understand the impact of their changes.
🌐
Go
go.dev › doc › effective_go
Effective Go - The Go Programming Language
There is no do or while loop, only a slightly generalized for; switch is more flexible; if and switch accept an optional initialization statement like that of for; break and continue statements take an optional label to identify what to break or continue; and there are new control structures including a type switch and a multiway communications multiplexer, select.
🌐
Reddit
reddit.com › r/learnpython › whats the difference between a while loop and a for loop?
r/learnpython on Reddit: Whats the difference between a while loop and a for loop?
July 15, 2024 -

I want to ask, whats the difference between looping using the while and the for function? I know both of these functions loops through stuff but what exactly is the difference between them? I have trouble understanding so I decide to ask this question on here

Find elsewhere
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter05.02-While-Loops.html
While Loops — Python Numerical Methods
The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon! ... A while loop or indefinite loop is a set of instructions that is repeated as long as the associated logical expression is true.
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
In Python, we use a while loop to repeat a block of code until a certain condition is met.
🌐
Tutedude
tutedude.com › category › python
Learn Python: Start Your Programming Journey with ...
Join Tutedude for expert mentorship and hands-on courses in coding, data science, UI/UX, and more. Upskill with a 100% refund policy. Start learning today!
🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - The while loop, on the other hand, doesn't run at least once and may in fact never run. It runs when and only when the condition is met. So, let's say we have an example where we want a line of code to run at least once. secret_word = "python" counter = 0 while True: word = input("Enter the secret word: ").lower() counter = counter + 1 if word == secret_word: break if word != secret_word and counter > 7: break
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - In Python, we can simulate the behavior of a do-while loop using a while loop with a condition that is initially True and then break out of the loop when the desired condition is met.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
Here's an infinite loop example caused by a typical looking bug — the variable i accidentally stays at the value 1 and the loop just goes forever. i = 0 while i < 10: # BUG infinite loop print(i) i = i * 1 print('All done')
🌐
W3Schools
w3schools.com › js › js_loop_while.asp
W3Schools.com
December 17, 2025 - W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
Python
wiki.python.org › moin › WhileLoop
While loops - Python Wiki
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list. ... While loops, like the ForLoop, are used for repeating sections of code - but unlike a for loop, the while loop will not run n times, but until a defined condition is no ...
🌐
Reddit
reddit.com › r/learnpython › is it weird that i never use while loops? it’s just not something i’ve felt like i needed, ever. what are some situations where using a while loop is necessary?
r/learnpython on Reddit: Is it weird that I NEVER use while loops? It’s just not something I’ve felt like I needed, ever. What are some situations where using a while loop is necessary?
September 29, 2022 -

I’ve written a lot of python code over the last few months, but I can’t think of a single time I’ve ever used a while loop. I mean, I use for loops pretty often, but not while loops.

Is this just down to personal preference, and I’m just using what I’m comfortable with? Or can you guys think of some situations where a while loop would be the easiest way to do things, but it’s possible to do it with a for loop? Maybe I’m using for loops in situations that I should be using while loops.

EDIT: Thanks for the suggestions. I found a few places in my code where a while loop makes sense.

First, I check if a folder has any files in it, and while it does, I delete the first one in the list:

useless_files = os.listdir("MGF_HR_Data") # I think these are files it creates when downloading the MGF data but they are extra and don't do anything 
while len(useless_files)>0: 
    os.remove(f"MGF_HR_Data/{useless_files[0]}") 
    useless_files = os.listdir("MGF_HR_Data") 
    print("Deleting useless file from MGF_HR_Data") 

I also used a while loop to check if the data has been downloaded, and if it hasn't prompt the user to download it and then press enter to check again. This way, the code doesn't break if the user forgot to download the file first:

# Check if you downloaded the file in Matlab already. If not, ask the user to download it and try again. 
while os.path.exists(file1)==False: 
    print(f"{file1} not found. Please open Matlab and run the following code to download the MGF data:" )
    print(f"download({wstime.year}, {wstime.month}, {wstime.day})")
    input("Press enter to try again after downloading the MGF data. \n") 

(Okay, I know this isn't the most efficient way to do this. There must be a way to use python to open matlab and run a matlab command, rather than asking the user to do it themselves. However, I don't know how to do that, so this works for now.)

🌐
W3Schools
w3schools.com › python › python_lists_loop.asp
Python - Loop Lists
Use the len() function to determine the length of the list, then start at 0 and loop your way through the list items by referring to their indexes. Remember to increase the index by 1 after each iteration. Print all items, using a while loop to go through all the index numbers
🌐
Server Academy
serveracademy.com › blog › python-while-loop-tutorial
Python While Loop Tutorial - Server Academy
November 12, 2024 - A while loop in Python repeatedly executes a block of code as long as a specified condition remains true.
🌐
Analytics Vidhya
analyticsvidhya.com › home › all about python while loop with examples
Python While Loop with Examples and Use Cases
January 31, 2024 - In this example, we initialize the variable `num` to 1. The while loop continues if `num` is less than or equal to 5. Inside the loop, we print the value of `num` and then increment it by 1 using the `+=` operator. This process repeats until `num` becomes 6, when the condition becomes False, and the loop terminates. Python provides three loop control statements, ‘ break’, ‘ continue’, and’ pass, ‘ to allow you to control the flow of a while loop.
🌐
Python.org
discuss.python.org › python help
Need help understanding this while loop - Python Help - Discussions on Python.org
February 13, 2024 - I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number <= 5: print(current_number) current_number += 1 After just watching a video on Yo…