Iteration, while loop, and for loop

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Tue Jun 28 09:42:39 EDT 2016


Elizabeth Weiss writes:

[- -]

> What I do not understand is:
>
> words=["hello", "world", "spam", "eggs"]
> counter=0
> max_index=len(words)-1
>
> while counter<=max_index:
>    word=words[counter]
>    print(word + "!")
>    counter=counter + 1

# make it so that counter == 0
counter=0

# make it so that max_index is the greatest valid index to words (unless
# words is empty -- it would be simpler and idiomatic to make end_index
# be the first invalid index aka len(words), but make your life that
# little bit harder because ... reasons?)
max_index=len(words)-1

while counter<=max_index:

    # Since we reached this point, counter is at most max_index, and is
    # a valid index unless words is empty. If the test was instead
    # counter < end_index, it would definitely be valid, or we wouldn't
    # be *here*. Anyway, the first time at this point, counter == 0, the
    # second time counter == 1, ...

    # Set word to the item of words indicated by counter. First time at
    # this point, words[0], second time, words[1], ...; print it.
    word=words[counter]
    print(word + "!")

    # Make it so that counter is one larger than it used to be. In
    # particular, it is closer to being larger than max_index so that we
    # are eventually done with this loop.
    counter=counter + 1

    # At this point, the loop condition is tested again with the new
    # value of counter(*) and when that new value is such it is no
    # longer the case that counter<=max_index, looping ends.

# When we reach this point, the loop condition is false (or there has
# been another kind of exit from the loop): counter == max_index + 1.

(*) Now the thread may go into yet another tangent on how that "new
value" is really not a value but is the value of an immutable object
that is the value of a reference pointer in a box that is tied to a
puppy that is dancing on the point of a pin. You may or may not find
that helpful. Also, one or more of these things may be a name or have a
name.

> Both of these result in the same answer. 
> I do not understand the second code. What is counter?
> Why do we use this code if we can use the simpler for loop? 

Some loops are not easily written as for loops. This is not one of them.

For loops are easy to rewrite as while loops, so while loops may be
considered theoretically more fundamental. Other theoretical models
would express while loops with even more fundamental mechanisms (or they
can be quite practical models, though not directly available in Python
for this purpose or at all: conditional jumps to a labelled region of
program code, or function calls).

You need to get used to how program elements like counter behave. There
are events in the execution of a program that make it so that such
program elements stand for particular things (like numbers) in a
dependable way, in certain specific regions of code, until some new
event. The statement involving a single equals sign is one such event;
each entry to a for loop is another; a function call is yet another.

(I see that I have used the word "thing" about things that I really do
think of as things in the context of a Python program, and of things
that I don't. [REDACTED] it's hard to talk about these, er, things.
Please ignore this paragraph, and that paragraph marked with (*).)

[- -]



More information about the Python-list mailing list