Python for loop question

Python’s for loops are different. i gets reassigned to the next value every time through the loop.

The following will do what you want, because it is taking the literal version of what C++ is doing:

i = 0
while i < some_value:
    if cond...:
        i+=1
    ...code...
    i+=1

Here’s why:

in C++, the following code segments are equivalent:

for(..a..; ..b..; ..c..) {
    ...code...
}

and

..a..
while(..b..) {
     ..code..
     ..c..
}

whereas the python for loop looks something like:

for x in ..a..:
    ..code..

turns into

my_iter = iter(..a..)
while (my_iter is not empty):
    x = my_iter.next()
    ..code..

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)