Increasing the value of a for loop counter-Collection of common programming errors

  • Variable, like in C

    In C, this loop leads to effectively making steps of 2, as obvious.

  • Iterator, like in Python

    In Python, a loop like this gets fed by an iterator which constantly yields new values independent of the variable.

    for i in range(0, 100):
       print "A:", i
       i += 1
       print "B:", i
    

    leads to

    A: 0
    B: 1
    A: 1
    B: 2
    ....
    

    while your loop in C, provided with the correct outputs, would lead to

    A: 0
    B: 1
    A: 2
    B: 3
    ....
    

Originally posted 2013-11-09 22:48:24.