Re: [Tutor] Loop Variables

Magnus Lycka magnus at thinkware.se
Thu Jun 24 11:15:16 EDT 2004


> Hi, I had come to read a book about loop variable that have i, j, k.
> I don't understand why are there 3 type?

In general, variable names should use descriptive names, so calling 
any variable i, j or k is probably a poor decision. It doesn't tell 
us anything about the purpose of the code.

One might say that i, j, etc are well established as loop variable
names, but typically, the loop variables in loops represent something
meaningful which we have a name for. Depending on context, better loop
varable names might be 'row', 'player', 'transaction' or whatever we
are dealing with in the program.

There is a conflict for those who write educational code examples.
On one hand, you don't want to distract the reader with a lot of
irrelevant context (such as long variable names with a meaning which
is irrelevant for the detail you want to explain). On the other hand,
by constantly using really bad variable names like i,j,k and a,b,c,
you teach your readers a bad programming habit.

The typical reasons for using several loop variables such as i, j, k is 
that you nest loops. You have several loops inside each other, and you
need to keep track of your position in these loops independently of each
other. For instance, imagine that you want to display a multiplication 
table:

def mult(max_x, max_y):
    print '    *',
    for x in range(1, max_x + 1):
        print "%5i" % x,
    print
    for y in range(1, max_y + 1):
        print "%5i" % y,
        for x in range(1, max_x + 1):
            print "%5i" % (x * y),
        print


>>> mult(8,10)
    *     1     2     3     4     5     6     7     8
    1     1     2     3     4     5     6     7     8
    2     2     4     6     8    10    12    14    16
    3     3     6     9    12    15    18    21    24
    4     4     8    12    16    20    24    28    32
    5     5    10    15    20    25    30    35    40
    6     6    12    18    24    30    36    42    48
    7     7    14    21    28    35    42    49    56
    8     8    16    24    32    40    48    56    64
    9     9    18    27    36    45    54    63    72
   10    10    20    30    40    50    60    70    80

I guess you understand that you need to use two different variables 
for x and y here. I do use x two times in different loop as you see.
For me, x is the x-axis (i.e. horizontal) values, and y is the y-axis
(vertical) values. I guess I could have called y 'row', and x 'col'
instead, but x and y are well established terms in this context.

You should also note that code blocks don't imply scopes in Python
as they do in i.e. C++. So, even if you don't need to access a loop
varible in any inner loop, using the same loop variable in nested
loops will mess things up. See below:

>>> for i in range(2):
        for j in range(2):
                for k in range(2):
                        print '    Inner', k
                print '  Middle', j
        print 'Outer', i

	
    Inner 0
    Inner 1
  Middle 0
    Inner 0
    Inner 1
  Middle 1
Outer 0
    Inner 0
    Inner 1
  Middle 0
    Inner 0
    Inner 1
  Middle 1
Outer 1
>>> for i in range(2):
        for i in range(2):
                for i in range(2):
                        print '    Inner', i
                print '  Middle', i
        print 'Outer', i

	
    Inner 0
    Inner 1
  Middle 1    <= BUG!
    Inner 0
    Inner 1
  Middle 1
Outer 1       <= BUG!
    Inner 0
    Inner 1
  Middle 1    <= BUG!
    Inner 0
    Inner 1
  Middle 1
Outer 1

You will still get the right number of iterations, since the
range functions will produce the right number of elements
anyway. A C style for loop such as "for (i=0; i<2; i++)" 
would end prematurely if the same loop variable was used.
Python is more robust, but things can still go wrong... :)

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list