Michael Torrie torriem at
Fri Jul 25 07:37:50 CEST 2008

You wrote:
> How to represent the loop
> for ($a = $b; $a<=$c;$a++){
> } in Python

As other pointed out, iterating through a list or range is often a far
more elegant way to do a loop than a C-style loop.  But the C-style for
loop is just syntactic sugar for a while loop.  In some cases, C-style
for loops can have an initializer, a set of conditions, and incrementer
parts that are all based on different variables.  For example:

for (a=begin_func() ; x < 3 and sometest(b) ; i=somefunc() )

This highly illogical and contrived function could not be represented in
python with a simple "for x in blah" statement.  Rather you have to
represent it in its true form, which is a while loop:

while x < 3 and sometest(b):
	#do stuff
	#loop body

In fact, the perl/c for loop of the form:

for (<initializer>;<condition>;<incrementer>)

always translates directly to:

while <condition>:
	#loop body


More information about the Python-list mailing list