I am not sure what you are trying to do. You can implement a do-while loop like this:
while True:
stuff()
if fail_condition:
break
Or:
stuff()
while not fail_condition:
stuff()
What are you doing trying to use a do while loop to print the stuff in the list? Why not just use:
for i in l:
print i
print "done"
Update:
So do you have a list of lines? And you want to keep iterating through it? How about:
for s in l:
while True:
stuff()
# use a "break" instead of s = i.next()
Does that seem like something close to what you would want? With your code example, it would be:
for s in some_list:
while True:
if state is STATE_CODE:
if "//" in s:
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = STATE_COMMENT
else :
tokens.add( TOKEN_CODE, s )
if state is STATE_COMMENT:
if "//" in s:
tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
break # get next s
else:
state = STATE_CODE
# re-evaluate same line
# continues automatically
Answer from Tom on Stack Overflowpython - How to emulate a do-while loop? - Stack Overflow
As a beginner how do I understand while loops?
Need help understanding this while loop
Why not add do-while loops to Python?
Videos
I am not sure what you are trying to do. You can implement a do-while loop like this:
while True:
stuff()
if fail_condition:
break
Or:
stuff()
while not fail_condition:
stuff()
What are you doing trying to use a do while loop to print the stuff in the list? Why not just use:
for i in l:
print i
print "done"
Update:
So do you have a list of lines? And you want to keep iterating through it? How about:
for s in l:
while True:
stuff()
# use a "break" instead of s = i.next()
Does that seem like something close to what you would want? With your code example, it would be:
for s in some_list:
while True:
if state is STATE_CODE:
if "//" in s:
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = STATE_COMMENT
else :
tokens.add( TOKEN_CODE, s )
if state is STATE_COMMENT:
if "//" in s:
tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
break # get next s
else:
state = STATE_CODE
# re-evaluate same line
# continues automatically
Here's a very simple way to emulate a do-while loop:
condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop
The key features of a do-while loop are that the loop body always executes at least once, and that the condition is evaluated at the bottom of the loop body. The control structure show here accomplishes both of these with no need for exceptions or break statements. It does introduce one extra Boolean variable.
While loops is kinda frustrating I'm 20 days into python and I'm stuck on loops since last 4 days
It continues to puzzle me why we have no do-while loop in Python.
In Python code, it's common to see the following pattern:
# ... some code ....
while condition:
# ... same code copied here ...This is of course prone to problems because of the "minor" code duplication.
Alternatively, you might see this better option:
def some_code():
# ... some code ...
some_code()
while condition:
some_code()This involves creating a function even though IMHO it often serves to bloat the code unnecessarily.
And than there's this variation:
while True:
# ... do something ...
if not condition:
breakIMHO, this approach, especially when the body of the loop is fairly large, fails to communicate the logical intent of the code in a clean manner.
Of course - all of these approaches do work. But IMHO a syntax which could clarify programmer's intent in the most precise and concise way is the following classical do-while:
do:
# ... do something ...
while some_conditionI know that years ago do-while style constructs have been proposed in PEPs and rejected.
But isn't it time we review the idea again? I think there is a considerable amount of code which could benefit from this.
Would love to hear your thoughts :)