Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "do ... until" in Python? [duplicate]

Tags:

python

loops

Is there a

do until x:
    ...

in Python, or a nice way to implement such a looping construct?

like image 536
Matt Joiner Avatar asked Nov 02 '09 16:11

Matt Joiner


People also ask

Is there a repeat until function in Python?

The repeat / until loop is a loop that executes a block of statements repeatedly, until a given condition evaluates to true . The condition will be re-evaluated at the end of each iteration of the loop, allowing code inside the loop to affect the condition in order to terminate it.

How do you repeat a Python code until a condition is met?

What is a while loop in Python? A while loop will run a piece of code while a condition is True. It will keep executing the desired set of code statements until that condition is no longer True. A while loop will always first check the condition before running.

Is there an until statement in Python?

Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed.

Is there a do while loop in Python?

Python doesn't have do-while loop. But we can create a program like this. The do while loop is used to check condition after executing the statement. It is like while loop but it is executed at least once.


4 Answers

There is no do-while loop in Python.

This is a similar construct, taken from the link above.

 while True:
     do_something()
     if condition():
        break
like image 171
theycallmemorty Avatar answered Sep 29 '22 11:09

theycallmemorty


I prefer to use a looping variable, as it tends to read a bit nicer than just "while 1:", and no ugly-looking break statement:

finished = False
while not finished:
    ... do something...
    finished = evaluate_end_condition()
like image 42
PaulMcG Avatar answered Sep 29 '22 12:09

PaulMcG


There's no prepackaged "do-while", but the general Python way to implement peculiar looping constructs is through generators and other iterators, e.g.:

import itertools

def dowhile(predicate):
  it = itertools.repeat(None)
  for _ in it:
    yield
    if not predicate(): break

so, for example:

i=7; j=3
for _ in dowhile(lambda: i<j):
  print i, j
  i+=1; j-=1

executes one leg, as desired, even though the predicate's already false at the start.

It's normally better to encapsulate more of the looping logic into your generator (or other iterator) -- for example, if you often have cases where one variable increases, one decreases, and you need a do/while loop comparing them, you could code:

def incandec(i, j, delta=1):
  while True:
    yield i, j
    if j <= i: break
    i+=delta; j-=delta

which you can use like:

for i, j in incandec(i=7, j=3):
  print i, j

It's up to you how much loop-related logic you want to put inside your generator (or other iterator) and how much you want to have outside of it (just like for any other use of a function, class, or other mechanism you can use to refactor code out of your main stream of execution), but, generally speaking, I like to see the generator used in a for loop that has little (ideally none) "loop control logic" (code related to updating state variables for the next loop leg and/or making tests about whether you should be looping again or not).

like image 38
Alex Martelli Avatar answered Sep 29 '22 12:09

Alex Martelli


No there isn't. Instead use a while loop such as:

while 1:
 ...statements...
  if cond:
    break
like image 37
f0b0s Avatar answered Sep 29 '22 13:09

f0b0s