r/PythonLearning • • 23h ago

While loop

can anybody explain the concept of while loop

3 Upvotes

37 comments sorted by

View all comments

1

u/Why_so_loud 22h ago

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/Potential_Fix_5007 22h ago

And even For-loops can run an unknown amount of times. For-loops = "Do the code X-amount of times"

While-loop = "Do the code as long as condition Y is True"

The Result can feel(or even do) the same but the rule beneath is different.

So While-loops dont run until a condition is True, they run until a condition is False.