newbie question

John Machin sjmachin at lexicon.net
Fri Feb 11 18:00:02 EST 2005


Dan Perl wrote:
> <doodle4 at gmail.com> wrote in message
> news:1107990182.516041.120410 at o13g2000cwo.googlegroups.com...
> > 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

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




More information about the Python-list mailing list