[Tutor] Differences between while and for

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Sat Jun 15 05:27:01 EDT 2019


Steven

Thanks. 

-----Original Message-----
From: Tutor <tutor-bounces+mhysnm1964=gmail.com at python.org> On Behalf Of
Steven D'Aprano
Sent: Saturday, 15 June 2019 5:25 PM
To: tutor at python.org
Subject: Re: [Tutor] Differences between while and for

On Sat, Jun 15, 2019 at 02:53:43PM +1000, mhysnm1964 at gmail.com wrote:
> All,
> 
>  
> 
> 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.

Python ``while`` uses a conditional statement, and Python ``for`` uses
iteration. Python's ``for`` is like "foreach" in some other languages.

while condition: ...

for x in values: ...


> 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?

Correct, there is no "do until" in Python.

> Why doesn't Python have an until statement?

Because Guido didn't want one :-)

Because it is unnecessary: any "do until" can be written as a regular while
loop, using a break:

# do...until with test at the end
while True:
    do_something()
    if test:
       break


# "loop and a half"
# https://users.cs.duke.edu/~ola/patterns/plopd/loops.html#loop-and-a-half
while True:
    do_something()
    if test:
        break
    do_something_else()



--
Steven
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list