# [Tutor] Re: Loop Variables

Lee Harr missive at hotmail.com
Thu Jun 24 12:27:35 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?
>What's the different?
>

It would help to see some of the code that uses the variable,
so that we can see exactly why they have chosen to use those
names.

As far as I can tell from what you have written, there are not
really 3 different types, just 3 different variables.

They are just 3 different storage areas for data that you want
to keep track of.

Let's say you want to count up to 10 twice.

>>>for count_twice in range(2):
...     for up_to_10 in range(1, 11):
...         print 'this is count', count_twice, 'at number', up_to_10
...
this is count 0 at number 1
this is count 0 at number 2
this is count 0 at number 3
this is count 0 at number 4
this is count 0 at number 5
this is count 0 at number 6
this is count 0 at number 7
this is count 0 at number 8
this is count 0 at number 9
this is count 0 at number 10
this is count 1 at number 1
this is count 1 at number 2
this is count 1 at number 3
this is count 1 at number 4
this is count 1 at number 5
this is count 1 at number 6
this is count 1 at number 7
this is count 1 at number 8
this is count 1 at number 9
this is count 1 at number 10

But we could have written it like this ...

>>>for i in range(2):
...     for j in range(1, 11):
...         print 'this is count', i, 'at number', j
...
this is count 0 at number 1
this is count 0 at number 2
this is count 0 at number 3
this is count 0 at number 4
this is count 0 at number 5
this is count 0 at number 6
this is count 0 at number 7
this is count 0 at number 8
this is count 0 at number 9
this is count 0 at number 10
this is count 1 at number 1
this is count 1 at number 2
this is count 1 at number 3
this is count 1 at number 4
this is count 1 at number 5
this is count 1 at number 6
this is count 1 at number 7
this is count 1 at number 8
this is count 1 at number 9
this is count 1 at number 10

Same result.

But we definitely need 2 different variables. If we tried to use the
same variable twice, it would not work ...

>>>for i in range(2):
...     for i in range(1, 11):
...         # not really sure what to put here ...
...         print 'this is count', i, 'at number', i
...
this is count 1 at number 1
this is count 2 at number 2
this is count 3 at number 3
this is count 4 at number 4
this is count 5 at number 5
this is count 6 at number 6
this is count 7 at number 7
this is count 8 at number 8
this is count 9 at number 9
this is count 10 at number 10
this is count 1 at number 1
this is count 2 at number 2
this is count 3 at number 3
this is count 4 at number 4
this is count 5 at number 5
this is count 6 at number 6
this is count 7 at number 7
this is count 8 at number 8
this is count 9 at number 9
this is count 10 at number 10

Which (surprisingly enough to me) works better than
expected. Of course the output is nonsense.

I guess what happens is that the second "for loop" is
creating a new "namespace". So the second use of
x is actually a different variable than the first use of x.

If that seems confusing, I guess I would just say
"don't do that"  :o)

It is almost always better to try to come up with some
meaningful name for your variables (rather than just
use i, j, k) Of course for every rule ...

If it is just a counter, and the value will only be used in
one small section of code, then i, j or k might be perfectly
reasonable variable names.

_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE*
http://join.msn.com/?page=features/virus

```