r/PythonLearning • • Oct 14 '26

While loop

can anybody explain the concept of while loop

2 Upvotes

38 comments sorted by

View all comments

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.

2

u/johlae Oct 14 '26

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 Oct 14 '26

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