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 :)
There is no do...while loop because there is no nice way to define one that fits in the statement: indented block pattern used by every other Python compound statement. As such proposals to add such syntax have never reached agreement.
Nor is there really any need to have such a construct, not when you can just do:
while True:
# statement(s)
if not condition:
break
and have the exact same effect as a C do { .. } while condition loop.
See PEP 315 -- Enhanced While Loop:
Rejected [...] because no syntax emerged that could compete with the following form:
while True: <setup code> if not <condition>: break <loop body>A syntax alternative to the one proposed in the PEP was found for a basic do-while loop but it gained little support because the condition was at the top:
do ... while <cond>: <loop body>
or, as Guido van Rossum put it:
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.
Because everyone is looking at it wrong. You don't want DO ... WHILE you want DO ... UNTIL.
If the intitial condition is true, a WHILE loop is probably what you want. The alternative is not a REPEAT ... WHILE loop, it's a REPEAT ... UNTIL loop. The initial condition starts out false, and then the loop repeats until it's true.
The obvious syntax would be
repeat until (false condition):
code
code
But for some reason this flies over everyone's heads.
python - How to emulate a do-while loop? - Stack Overflow
We can use do - while loop in python .True or false .
How do while not loops work
Write True or False:
1. We can use do-while loop in Python.
2.
The continue statement breaks the loops one by one.
3.
4.
To come out of the infinite loop, we can either close the program window or p
Ctrl + C.
A sequence is a succession of values bound together by a single name.
5. The while statement is the looping statement.
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.
I was reading through this code and I'm just not getting how while loops work when not operator is also used.
https://pastebin.com/5mfBhQSb
I thought the not operator just inversed whatever the value was but I just can't see this code working if that's the case.
For example , the while not sequenceCorrect should turn the original value of False to True, so the loop should run while it's True. But why not just state while True then? And why declare sequenceCorrect = True again? Doesn't it just means while True, make it True? And so on.
The only way it makes sense to me is of the while not loop always means False (just like the while always means as long as it's True) even if the value is supposed be False and should be inverted to True.
So, is that the case? Can anyone explain why it works like that?