newbie question

D

doodle4

Hello All,
What is the python equivalent of the following statement?

while (n--)

Thank you.
-d4
 
P

Peter Hansen

What is the python equivalent of the following statement?

while (n--)

Thank you.

That depends on a number of things, including whether "n"
is declared as "volatile", and whether you consider the
above as an executable piece of code (it is not), or just a
code snippet...

Roughly but somewhat facetiously speaking, the answer is this:

n = -1

(Maybe describing what you are trying to accomplish would
be more useful than showing snippets of code without
explaining what you are expecting it to do.)

-Peter
 
D

doodle4

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?

Thanks.
-d4
 
P

Peter Hansen

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?

Generally in Python you simply put the assignment-based
statement in a "while True:" block, and use "break" to
exit as required. For example, here is one way to
approach it in this case:

while True:
n = n - 1
if n == -1:
break

Having written the above, however, I'm uncertain whether this
sort of thing is really required in Python. Although I have
ported very little C code to Python (so I might be wrong), I
definitely have never seen anything resembling the above
pattern in Python code that I or others have written.

My suspicion is that the code involves operations which are
actually better handled in Python by some kind of builtin
or standard library operation. For example, often the
post-decrementing is part of a pointer operation involving
manipulating a string character-by-character. This is
effectively never required in Python, and will pretty much
always result in a program written at a much lower level
than what you should be shooting for in Python. But if
you want a direct port, then the above should do...

-Peter
 
D

Dan Perl

Hello All,
What is the python equivalent of the following statement?

while (n--)

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

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 python.
 
J

Jeff Shannon

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?

Roughly speaking, if you have a loop in C like

while (n--) {
func(n);
}

then the mechanical translation to Python would look like this:

while True:
n -= 1
if not n:
break
func(n)

However, odds are fairly decent that a mechanical translation is not
the best approach, and you may (as just one of many examples) be much
better off with something more like:

for i in range(n)[::-1]:
func(n)

The '[::-1]' iterates over the range in a reverse (decreasing)
direction; this may or may not be necessary depending on the
circumstances.

Jeff Shannon
Technician/Programmer
Credit International
 
C

Cameron Laird

.
.
.
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.

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.
 
D

Dan Perl

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.

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--)
 
D

Dennis Lee Bieber

for i in range(n)[::-1]:
func(n)

Shouldn't that be
func(i)
(the loop index?)
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... Let's see... n initialized at
3, while condition sees:

pre post function
3 2 2
2 1 1
1 0 0
0 exits

My code sees: range(3) gives 0, 1, 2

n1 func
0 3-0->3
1 3-1->2
2 3-2->1
exits

Hmm, need

func(n-1-n1)

--
 
P

Peter Hansen

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.

The OP didn't show how he was using the "while (n--)" at all,
so it can hardly be a clear illustration of how it's useful.
In fact, it's even possible it was entirely unnecessary in
the original code... at this point I'd be really interested
in seeing just what code is inside the "while" statement, and
possibly what follows it (if the following code relies on the
value of "n").
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--)

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).

The fact that it's so easy to get confused with post-decrement
is perhaps an excellent reason to keep it out of Python.

-Peter
 
D

Dan Perl

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).

You're right in your nit picking. I have to go back to using some C/C++ and
Java also, I don't want to forget them completely.
 
J

Jeff Shannon

Dennis said:
for i in range(n)[::-1]:
func(n)


Shouldn't that be
func(i)
(the loop index?)

You're right, that's what I *meant* to say. (What, the interpreter
doesn't have a "do what I mean" mode yet? ;) )
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 [...]

Given a need/desire to avoid extended slicing (i.e. being stuck with
an older Python, as I often am), I'd actually do this by changing the
input to range(), i.e.

for i in range(n, 0, -1): # ...

That (IMO) makes the decreasing-integer sequence a bit clearer than
doing subtraction in the function parameter list does. Actually, it's
possibly clearer than the extended slicing, too, so maybe this would
be the better way all around... ;)
I haven't done the detailed
analysis to properly set the end point...

And 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.

Jeff Shannon
Technician/Programmer
Credit International
 
D

Dennis Lee Bieber

And 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.
Yeah, though my background tends to be one which considers loop
indices to be loop-local, value indeterminate after exit...

--
 
J

Jeff Shannon

Dennis said:
Yeah, though my background tends to be one which considers loop
indices to be loop-local, value indeterminate after exit...

Well, even though I've programmed mostly in langauges where loop
indices to retain a determinate value after exit, I almost always
*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...

Jeff Shannon
Technician/Programmer
Credit International
 
J

John Machin

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

errrmmm it's a C while stmt with post-decrement; that's NOT the same
as:

for (; n; n--) ...

If n is initially 3, the loop body is executed with n set to 2,1,0 and
after exit n is -1.

The nearest Python equivalents of "while (n--) func(n);" are
(making the charitable assumption that initially n >= 0):

! while n:
! n -= 1
! func(n)

and

! for n in range(n-1, -1, -1):
! func(n)
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
python.

Unfortunately?? What are you missing? The ability to write cryptic crap
that you don't understand & that's so "powerful" that you can get into
deep kaka really quickly? Hope you weren't writing software for
embedded devices -- like heart pacemakers :)
 
D

Dennis Lee Bieber

*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...
Lacking that context in the sample, I naturally fell into the
"discard it after loop" mode <G>

--
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,118
Members
47,737
Latest member
CarleyHarm

Latest Threads

Top