Hi.....I am a newbie...really confused with do while loop in python. Does it even exist?

J=[]
    do:
        finished = "false"
        while finished != "true":
            finished="true"
            for j in xrange(len(lis2)):
                  cenP2 = G[i+G0]
                   if ((cenP2 == 0) & (sigma2 > 0)  
                          J.append(1)
                          finished = "false"
                          print 3 # test code never gets here
                   else:
                          finished="true"
                         print 4

Sorry, I left out a few details caz its not important and just takes up space. My point is....how do I do this? I know this is not right, but is there a way?

Thanks a lot .....

Dani AI

Generated

Python does not have a built-in do...while, but you can emulate "run at least once, then decide" cleanly. Use a boolean updated at the end of each iteration (avoid strings like "true"/"false", and prefer logical and over bitwise &):

finished = False
while not finished:
    # loop body
    # ...
    finished = should_stop()  # compute this after doing the work

If the body is sizable, factor it into a function so you do not duplicate code:

def step():
    # one iteration of work
    pass

step()                # run once
while not done():     # then continue while condition holds
    step()

For the side question about popping from multiple lists until all are exhausted, loop while each list still has items. Popping from the end is O(1); if you need queue behavior, use collections.deque:

while L1 and L2 and L3:
    z1 = L1.pop()
    z2 = L2.pop()
    z3 = L3.pop()
    # process z1, z2, z3

Notes:

  • Use True/False booleans, not strings.
  • Use and/or, not &/|, for logical tests.
  • In modern Python, range() replaces xrange() and print() is a function.

References:

Recommended Answers

All 7 Replies

No, there is no "do ... while" loop in Python. A properly constructed while loop can do the same.

If you have any problems, give us a simplified idea of what you want to accomplish. We will be glad to help!

I have a doubt in while loop that suppose i want to do one looping process to assign values, how I have to do.
For example:
Having one list like L1 =
need to do pop operation and assign value like,
z1 = L1.pop()
z2 = L2.pop()
z3 = L3.pop()
...
similarly upto length of the list. Please give a solution.

Please do not hijack old threads, start your own new thread!

A properly constructed while loop can do the same.

Even a language with just if and goto constructs can do the same. However it is a matter of clarity and convenience if we have stopped using those 40 years ago.
None of the proposed solutions is as clear or elegant as what it could be if simply they added the do keywoard to Python.

There was a PEP about this, https://www.python.org/dev/peps/pep-0315/ . It was discussed and rejected by Guido Van Rossum who gave the reason:

Please reject the PEP. More variations along these lines won't make the language more elegant or easier to learn. They'd just save a few hasty folks some typing while making others who have to read/maintain their code wonder what it means.

I support the while True ... break construct. It is even more general than the do ... while.

Apart from that, this is a veeeeeeeeeeeery old thread. Don't resurrect old threads, start your own !

The elegant thing about the while True loop is that you can break at a given point between statements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.