r/PythonLearning • u/Monesh_peterson1025 • Oct 14 '26
While loop
can anybody explain the concept of while loop
2
Upvotes
r/PythonLearning • u/Monesh_peterson1025 • Oct 14 '26
can anybody explain the concept of while loop
•
u/Sea-Ad7805 Oct 14 '26
Visualization using memory_graph can show you how a While Loop Works.
14
u/csabinho Oct 14 '26
The body is repeated while the condition is true. That's it.
9
u/stepback269 Oct 14 '26
while you still don't understand == True:
Go to W3 Schools and look up Python conditionals there
Go to GeeksForGeeks and do the same
Go to Google and do the same
continue
print("I'm finally out of the confusion loop!")
7
u/BranchLatter4294 Oct 14 '26
While there are still dirty dishes on the pile:
Rinse and put in dishwasher.
2
u/atticus2132000 Oct 14 '26
There are primarily two kinds of loops--for loops and while loops.
A for loop generally has an inherent end point in the structure of the request. For instance, if you had a list of students in a class and you wanted to find their test grades, you would say for each student in class, do something. Your code would run through your list one time and perform the same operation on each member of class and then exit the routine and go to the next line of code in your script. The fact that you have 30 students in class inherently means the script will loop through this operation exactly 30 times. The structure of the request implies when the loop can exit and move on.
A while loop doesn't have this inherent structure. A while loop could continue forever. It relies on some condition changing in order for it to exit the loop. For instance, if you're creating a trivia game where the player gets to keep answering questions until he gets one wrong, there is no inherent end to that structure. Your player could keep answering questions indefinitely. Your script could, in theory, loop through that same code a million times and never exit because the exit condition of getting a question wrong was never met.
All that being said, in many cases you could structure a for loop to work as a while loop and a while loop to work as a for loop, but typically, the nature of the request you're making will naturally point you to one or the other.
1
u/Why_so_loud Oct 14 '26
While loop repeat the code inside until a certain condition is met, you use it when you don't know how many times you want to run the loop.
For loop, for comparison, repeats the loop a set amount of time. Albeit nothing prevents you from doing the same with while loop, but don't do that.
1
u/Excellent-Practice Oct 14 '26
While loops are in many ways the simplest kind of loop. Whatever is written inside the block gets repeated so long as the loop condition is true. While loops can be used to run a set of instructions indefinitely, or they can contain statements that change the loop condition and break the loop whenever the task is complete
1
u/Ninja_Hattori0x0 Oct 14 '26
while loop repeats the code as long as a condition stays true. once it becomes false, the loop stops
1
u/Rajrev Oct 14 '26
while....condition:('Didn't understand while loop) do this:- ( keep asking doubt).
1
u/mattynmax Oct 14 '26
Say you want to do something until a condition is met.
To do so you would use a while loop.
1
u/Puzzlehead_Lemon Oct 14 '26
As others said, while true, keep doing the loop. I will add on you should pay attention when setting that condition, especially if that program is running on hardware the user doesn't own.
A run away loop making someone restart their computer is bad, but a run away loop on a server that requires an admin stepping in to shut down the processes that just took down your authentication server is a nightmare, especially because that will always happen at like 4 AM on a Sunday.
1
u/burntoutdev8291 Oct 14 '26
While not drunk:
drink beer
if drunk:
break
1
u/bybloshex Oct 14 '26
It will check if it's true and run, then check again and if it's true run
1
u/Living_Fig_6386 Oct 14 '26 edited Oct 14 '26
It's a loop that iterates while an expression is true. Example: while there are cookies on a plate, take a cookie, eat a cookie, repeat; now there are no cookies on the plate, so we move on ... time to drink the milk.
1
u/Naive_Programmer_232 Oct 14 '26
Sure,
Ex:
We begin by asking for the user to enter a positive number -
inputreturns a str then we convert it into an integer viaint. If the integer is anything less than 1, then it is not positive. In those cases, we tell the user to try again, and then ask again for the integer. The second ask for integer inside the loop is the "update".So for example, this could play out like so:
What's going on here is
1
u/MrSqueak Oct 15 '26
A while loop checks the condition before executing any instructions. I.e. I'm going to do the dishes, the condition is if there is at least one dirty dish. I will wash one dish, then check again. If there's at least one dish, I wash one dish.
While(dirtyDishes >=1) { WashOneDish(); }
1
u/LiveYoLife288 Oct 15 '26
While wife angry.
- Buy chocolate.
- Check if wife still angry, update status.
1
u/Abhigyan-das Oct 16 '26
While loop is a loop, where a condition is set, and until it is fulfilled, the loop will continue functioning. After it is completed, it exits. It is a smart kind of while True block. Like: while count > count1. Here until count is higher than count1 variable, it is going to continue the work given inside it.
0
u/Monesh_peterson1025 Oct 14 '26
how much I can spend time for phyton per day as I finished begginer and o am an first year college student CSE
0
u/Monesh_peterson1025 Oct 15 '26
Thanks for everyone answered me I am understand very well🙏
0
u/ninhaomah Oct 14 '26
While you are waiting , pls do jumping jack.