Loop variable value after loop finish 
Author Message
 Loop variable value after loop finish

Hello All!

Can I safely trust that the value printed by the following program will
always be loops-1, except when loops<1? I wasn't fully convinced by the
docs on this point.

for i in range(loops):
    pass
print i

Have a nice day,
    Mikael J.
    for email correct domain



Mon, 02 Sep 2002 03:00:00 GMT  
 Loop variable value after loop finish

Quote:

> Can I safely trust that the value printed by the following program will
> always be loops-1, except when loops<1? I wasn't fully convinced by the
> docs on this point.

> for i in range(loops):
>     pass
> print i

Well, my copy of the reference manual says (in part):

----------------------------------------------------------
"""
for_stmt: "for" target_list "in" expression_list ":" suite
          [ "else" ":" suite ]

The expression list is evaulated once; it should yield a sequence. The suite
is then executed once for each item in the sequence, in the order of
ascending indices. Each item in turn is assigned to the target list using
the standard rules for assignments and then the suite is executed. [etc]
"""
----------------------------------------------------------

which seems to define the behaviour sufficiently to me - in other words,
yes.

In more detail:

"""The expression list is evaulated once; it should yield a sequence."""
- yes - the sequence [0,...,loops-1] as defined for "range".

"""The suite is then executed once for each item in the sequence, in the
order of ascending indices."""
- ok - that's the obvious order. and we know we don't have the empty
sequence because you excluded that case.

"""Each item in turn is assigned to the target list using the standard rules
for assignments"""
- so "i" becomes 0, ... loops-1 in turn

"""and then the suite is executed."""
- which is "pass", so that doesn't change anything.

So the result is as you expect - heh presto.

Tibs (give me a manual and a pedant and I can - well, irritate the world, I
guess)
--
Tony J Ibbs (Tibs)      http://www.tibsnjoan.demon.co.uk/
Well we're safe now....thank God we're in a bowling alley.
- Big Bob (J.T. Walsh) in "Pleasantville"
My views! Mine! Mine! (Unless Laser-Scan ask nicely to borrow them.)



Mon, 02 Sep 2002 03:00:00 GMT  
 Loop variable value after loop finish

Hello Tony!


[...]

Quote:
> which seems to define the behaviour sufficiently to me - in other words,
> yes.

OK, thanks! I just wondered if "i" maybe wasn't guaranteed to be more
than some internal variable of the for-loop, even if all my tests showed
it had the expected value (and a value at all) after a loop.

Have a nice day,
    Mikael J.
    for email correct domain



Mon, 02 Sep 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. changing the value of loop-control variable in loop

2. display values while looping through a for loop

3. assigning to do loop control variables inside the loop

4. add values to one variable in a loop

5. changing the value of the loop control variable.

6. changing value of loop control variable.

7. Variable wire/reg value in a for-loop with a jump/skip

8. Do loop problem: iteration variable suddenly changing value

9. Stoping For loop before it's finished.

10. break one loop and skip one iteration of outer loop

11. 2 files: a loop within a loop??

12. Problem with loop inside other loop

 

 
Powered by phpBB® Forum Software