how to write a C-style for loop?

J

John Salerno

I assume this is the way for loops are written in C, but if it helps to
be specific, I'm referring to C# for loops. The Python for loop seems to
be the same (or similar) to C#'s foreach loop:

foreach int i in X

But how would you write a C# for loop in Python? Do you rework a while
loop, or use the range() function?

Here's an example:

for (int i = 0; i < 50; i += 5)

How would that go in Python, in the simplest and most efficient way?

Thanks.
 
D

Dylan Moreland

John said:
I assume this is the way for loops are written in C, but if it helps to
be specific, I'm referring to C# for loops. The Python for loop seems to
be the same (or similar) to C#'s foreach loop:

foreach int i in X

But how would you write a C# for loop in Python? Do you rework a while
loop, or use the range() function?

Here's an example:

for (int i = 0; i < 50; i += 5)

How would that go in Python, in the simplest and most efficient way?

Thanks.

Take a look at the range() builtin. That should give you what you need.
 
T

Tim Roberts

John Salerno said:
I assume this is the way for loops are written in C, but if it helps to
be specific, I'm referring to C# for loops. The Python for loop seems to
be the same (or similar) to C#'s foreach loop:

foreach int i in X

But how would you write a C# for loop in Python? Do you rework a while
loop, or use the range() function?

Here's an example:

for (int i = 0; i < 50; i += 5)

How would that go in Python, in the simplest and most efficient way?

for i in range(0,50,5):
print i
 
Z

ZeD

Ciao, John Salerno! Che stavi dicendo?
for (int i = 0; i < 50; i += 5)

How would that go in Python, in the simplest and most efficient way?

i=0
while i<50:
#...
i+=5

about range()/xrange(): what if you want to traslate this c-loop?
for (int i=1; i<50; i*=2)
 
S

Steven D'Aprano

Ciao, John Salerno! Che stavi dicendo?


i=0
while i<50:
#...
i+=5

That's exceedingly unPythonic. In fact I'd go far to say it is bad
practice in just about any programming language that has for loops. Why on
earth would any sensible programmer want to manage the loop variable by
hand if the language can do it for you?

about range()/xrange(): what if you want to traslate this c-loop? for
(int i=1; i<50; i*=2)

That's a completely different question, so of course it has a completely
different answer. Here is one way:

for i in [2**n for n in range(6)]:
do_something(i)

Here is another:

for i in range(int(math.log(50)/math.log(2)+1)):
do_something(2**i)

Here is a third way:

i = 1
while i < 50:
do_something(i)
i *= 2

Here is a fourth way:

import operator
def looper(start, (op, finish), (op2, x)):
n = start
while op(n, finish):
yield n
n = op2(n, x)

loop = looper(1, (operator.__lt__, 50), (operator.__mul__, 2))

for i in loop:
do_something(i)
 
D

Duncan Booth

Steven said:
That's a completely different question, so of course it has a completely
different answer. Here is one way:

.... various options snipped ...

and another way for use when you have more than one loop following this
pattern:

def powerrange(base, start=0, limit=0):
value = base**start
while value < limit:
yield value
value *= base


for i in powerrange(2,0,50):
dosomething(i)


Putting the question the other way round: how would you move the control
logic out of the loop in C?
 
J

John Salerno

Tim said:
for i in range(0,50,5):
print i

Thanks guys. I thought it might be something like this. In fact, the
chapter I just read dicussed how you use the range function with a for
loop, but I didn't quite see a connection with the C# for loop.
 
D

Dan Sommers

Putting the question the other way round: how would you move the
control logic out of the loop in C?

I have written many functions not unlike this one (untested):

void for_each_record( void (*callback)( RECORD * ), void *callback_data )
{
DATA *p;

for( p = the_list; p; p = p->next )
(*callback)( p->the_record, callback_data );
return;
}

where "the_list" comes from somewhere (probably static within the module
containing for_each_record, or the loop could access a database, or
whatever), and it is up to the callers to write their own callback
functions and manage their own callback_data. Obviously, in an actual
product, there would be error checking, options for the callback
function to abort the loop early, and other bells and whistles (e.g.,
logging options) depending on the application.

But now we're off topic for comp.lang.python.

Regards,
Dan
 
B

bearophileHUGS

Steven D'Aprano>That's a completely different question, so of course it
has a completely different answer. Here is one way:<

Other versions without the creation of a list:

for i in (2**n for n in xrange(6)):
do_something(i)

for i in (1<<n for n in xrange(6)):
do_something(i)

for i in xrange(6):
do_something(i<<i)

Bye,
bearophile
 

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,285
Messages
2,571,415
Members
48,107
Latest member
jigyasauniversity

Latest Threads

Top