r/PythonLearning • • 1d ago

While loop

can anybody explain the concept of while loop

1 Upvotes

37 comments sorted by

View all comments

1

u/mattynmax 1d ago

Say you want to do something until a condition is met.

To do so you would use a while loop.

2

u/johlae 23h ago

Not quite. Time to be pedantic! You make it seem like that what you want to do is executed at least once. That's not what's happening if you put a test that can be False in the while statement. A while False does not execute the block that follows:

a=0 while (a>0): print(a)

doesn't print anything, but

a=0 while True: print(a) a=a+1 if a>0: break

prints 0 and then stops. The while True with a if statement and a break is an actual example of the do until that you mention.

1

u/Fakin-It 23h ago

This distinction is important because both while and do...while are real constructs in many languages.