Basic question

Karlo Lozovina _karlo_ at _mosor.net
Sat May 12 12:46:15 EDT 2007


Cesar G. Miguel wrote:

> for j in range(10):
>   print j
>   if(True):
>        j=j+2
>        print 'interno',j
> 
> What happens is that "j=j+2" inside IF does not change the loop
> counter ("j") as it would in C or Java, for example.
> Am I missing something?

If you want that kind of behaviour then use a `while` construct:

j = 0
while j < 5:
     print j
     if True:
         j = j + 3
         print '-- ', j

If you use a for loop, for each pass through the foor loop Python 
assigns next item in sequence to the `j` variable.

HTH,
Karlo.



More information about the Python-list mailing list