[Tutor] Differences between while and for
Cameron Simpson
cs at cskk.id.au
Sat Jun 15 04:11:54 EDT 2019
On 15Jun2019 14:53, Sean Murphy <mhysnm1964 at gmail.com> wrote:
>In C, Perl and other languages. While only uses a conditional statement
>and
>for uses an iteration. In python while and for seems to be the same and I
>cannot see the difference.
No, they're really much as in other languages.
In general (most languages), a for loop is for iterating over some
collection or list. A while is not explicitly for iteration over some
collection, it is for repeating an action until some condition fails (or
the inverse of the condition is achieved).
Let's take C's for loop. It really is closely related to a while. That's
because C is a pretty low level language. Early C is almost like a
structured assembly language (well, it is a lot better, but it is
deliberately close to the underlying machine). So C's for loop goes:
for (setup; condition; advance)
statement-or-block
You can leave any of these out. It is equivalent to this while loop:
setup
while condition:
statement-or-block
advance
but it is almost always used for iteration:
s="foo"
for (i=0; s[i]; i++)
...
which counts "i" along the string "s". You _can_ use of for arbitrary
while loops, but idiomatically that is rarely done - it is conceptually
useful to use "for" for various kinds of iteration and "while" for more
arbitrary repetition.
Python is a bit more rigid. The "while" loop is just like "while" in
other languages: do something while a condition holds. But a "for" loop
in Python is inherently about iteration; it is defined as:
for variable in iterable:
suite
and applies to any "iterable", some object or expression which can be
iterated over. Any object which is iterable may be used. So it is very
oriented towards collections of various kinds: lists, tuples, dictionary
(iterates over the keys) and so on.
>Python does not have an until (do while) where
>the test is done at the end of the loop. Permitting a once through the loop
>block. Am I correct or is there a difference and if so what is it?
You're correct.
>Why doesn't Python have an until statement?
Basicly because it isn't necessary. It is usually easy enough to work
around the lack that nobody has made a conincing case (meaning nobody
has convinced the core developers). It would probably be written:
do:
...
while condition
in some form if it ever came in to avoid using an new keyword ("until").
It does sometimes take a little contortion to make a do/while loop into
a Python while - you usually have to perform some kind of hack to make
the condition initially true. In the extreme case you just treat the
first loop specially:
first = True
while first or the-actual-condition:
... do stuff ...
first = False
if you want to use "first" during the "do stuff". Or you could be a bit
more reliable and go:
first_test = True
while first_test or the-actual-condition:
first_test = False
... do stuff ...
putting the flag up the top next to the condition.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list