D
doodle4
Hello All,
What is the python equivalent of the following statement?
while (n--)
Thank you.
-d4
What is the python equivalent of the following statement?
while (n--)
Thank you.
-d4
What is the python equivalent of the following statement?
while (n--)
Thank you.
Thanks for the reply.
I am trying to convert some C code to python and i was not sure what
the equivalent python code would be.
I want to postdecrement the value in the while loop. Since i cannot use
assignment in while statements is there any other way to do it in
python?
Hello All,
What is the python equivalent of the following statement?
while (n--)
Thanks for the reply.
I am trying to convert some C code to python and i was not sure what
the equivalent python code would be.
I want to postdecrement the value in the while loop. Since i cannot use
assignment in while statements is there any other way to do it in
python?
has the conciseness of the C statement. The pre- and post-increment
and -decrement in C/C++/Java are very powerful and I miss them in python.
Cameron Laird said:.
.
.
Me, too.
Which is, I suspect, evidence for the incompleteness of our Pythonhood.
As Peter Hansen already hinted in this thread, an appetite for the
increment and related operators probably is a symptom that there's an
opportunity nearby to use an iterator or string method or such. C++
and Java wish they had it so good.
for i in range(n)[::-1]:
func(n)
Eeee.... sneaky... (I'm a bit behind on latest syntax additions)The '[::-1]' iterates over the range in a reverse (decreasing)
direction; this may or may not be necessary depending on the
circumstances.
Dan said:I can't say that is not part of the reason, but the example in the OP is a
clear illustration of cases where something like an increment/decrement
operator would be very useful.
OTOH, I was thinking of saying in my
previous posting that I prefer
for n in range(start, 0, -1):
to
n = start
while (n--)
I think that the first form is more readable, although that may be just me.
I would actually even prefer the 'for' statement in C to the 'while'
statement:
for (n=start; n<=0; n--)
Peter Hansen said:I'm not sure if it's just picking nits, but I'd like to
point out that neither of your alternatives is actually
equivalent to the while (n--) form... nor was Jeff
Shannon's attempt (in that case it leaves the loop with
n equal to 0, not -1).
Dennis said:
The '[::-1]' iterates over the range in a reverse (decreasing)
direction; this may or may not be necessary depending on the
circumstances.
Eeee.... sneaky... (I'm a bit behind on latest syntax additions)
I'd probably have coded something like
for n1 in range(n):
func(n-n1)
though, and note that I do admit it here [...]
I haven't done the detailed
analysis to properly set the end point...
Yeah, though my background tends to be one which considers loopAnd as Peter Hansen points out, none of the Python versions leave n in
the same state that the C loop does, so that's one more way in which
an exact translation is not really possible -- and (IMO again) further
evidence that trying to do an exact translation would be
ill-conceived. Much better to consider the context in which the loop
is used and do a looser, idiomatic translation.
Dennis said:Yeah, though my background tends to be one which considers loop
indices to be loop-local, value indeterminate after exit...
Dan said:Like other posters said, you should give more details with your question.
What do you mean by equivalent? The following is *functionally* equivalent:
for n in range(start, 0, -1):
Of course, there is always:
while n:
...
n -= 1
python.But unfortunately, no, I don't think there is an equivalent in python that
has the conciseness of the C statement. The pre- and post-increment
and -decrement in C/C++/Java are very powerful and I miss them in
Lacking that context in the sample, I naturally fell into the*treat* them as loop-local -- it just seems safer that way. But not
everyone does so, and especially with C while loops, often the point
is to keep adjusting the control variable until it fits the
requirements of the next section...
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.