I have just come across a site that discusses Python's 'for' and
'while' loops as having an (optional) 'else' structure.
At first glance I interpreted it as being a bit like the 'default'
structure in PHP's switch block... But the switch block isn't a loop,
so, I am now confused as to the reason for using 'else' with the for
and while loops...
A few quick tests basically show that statements in the else structure
are executed at the fulfillment of the loop's expression (ie, no
break).
Example:
for i in range(10):
print i
else:
print 'the end!'
0
1
2
3
4
5
6
7
8
9
10
the end! 7 1518
En Thu, 18 Oct 2007 23:44:27 -0300, Ixiaus <pa*******@comcast.net>
escribió:
I have just come across a site that discusses Python's 'for' and
'while' loops as having an (optional) 'else' structure.
At first glance I interpreted it as being a bit like the 'default'
structure in PHP's switch block... But the switch block isn't a loop,
so, I am now confused as to the reason for using 'else' with the for
and while loops...
A few quick tests basically show that statements in the else structure
are executed at the fulfillment of the loop's expression (ie, no
break).
A `while` loop tests a condition: if it evaluates to true, keep cycling;
if it is false, stop. The `else` clause is executed when the condition is
false, as in any `if` statement. If you exit the loop by using `break`,
the `else` part is not executed (because you didn't get out of the loop by
determining the condition falseness)
You can think of a `for` loop as meaning `while there are remaining
elements to be iterated, keep cycling` and the `else` clause applies when
there are no more elements. A `break` statement does not trigger the else
clause because the iteration was not exhausted.
Once you get the idea, it's very simple.
--
Gabriel Genellina
* Gabriel Genellina (Fri, 19 Oct 2007 00:11:18 -0300)
En Thu, 18 Oct 2007 23:44:27 -0300, Ixiaus <pa*******@comcast.net>
escribió:
I have just come across a site that discusses Python's 'for' and
'while' loops as having an (optional) 'else' structure.
At first glance I interpreted it as being a bit like the 'default'
structure in PHP's switch block... But the switch block isn't a loop,
so, I am now confused as to the reason for using 'else' with the for
and while loops...
A few quick tests basically show that statements in the else structure
are executed at the fulfillment of the loop's expression (ie, no
break).
A `while` loop tests a condition: if it evaluates to true, keep cycling;
if it is false, stop. The `else` clause is executed when the condition is
false, as in any `if` statement. If you exit the loop by using `break`,
the `else` part is not executed (because you didn't get out of the loop by
determining the condition falseness)
So a for/else loop is exactly the same thing as a for loop with the
else clause outside the loop (except for "break")? Guess that's why I
never used that...
Thorsten
On Oct 19, 3:12 am, Thorsten Kampe <thors...@thorstenkampe.dewrote:
So a for/else loop is exactly the same thing as a for loop with the
else clause outside the loop (except for "break")?
Am I missing something here? It sounds to me like you just described
two identical constructs.
Guess that's why I
never used that...
Thorsten
On 19 Okt, 13:39, Dustan <DustanGro...@gmail.comwrote:
On Oct 19, 3:12 am, Thorsten Kampe <thors...@thorstenkampe.dewrote:
So a for/else loop is exactly the same thing as a for loop with the
else clause outside the loop (except for "break")?
Am I missing something here? It sounds to me like you just described
two identical constructs.
Think of the loop-plus-else construct as behaving like this:
while 1:
# Get next element (in a for loop)
if loop_condition: # eg. whether we have an element
# Loop body statement
else:
# Loop else statement
break
Taking the example...
for i in range(10):
print i
else:
print 'the end!'
This is equivalent to...
while 1:
# Get next element (from the range iterator)
if next element: # yes, it's more complicated than this
print i
else:
print 'the end!'
break
Now consider what happens if you put a break statement inside the for
loop.
Paul
On Oct 19, 4:11 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 18 Oct 2007 23:44:27 -0300, Ixiaus <parnel...@comcast.net>
escribió:
I have just come across a site that discusses Python's 'for' and
'while' loops as having an (optional) 'else' structure.
At first glance I interpreted it as being a bit like the 'default'
structure in PHP's switch block... But the switch block isn't a loop,
so, I am now confused as to the reason for using 'else' with the for
and while loops...
A few quick tests basically show that statements in the else structure
are executed at the fulfillment of the loop's expression (ie, no
break).
A `while` loop tests a condition: if it evaluates to true, keep cycling;
if it is false, stop. The `else` clause is executed when the condition is
false, as in any `if` statement. If you exit the loop by using `break`,
the `else` part is not executed (because you didn't get out of the loop by
determining the condition falseness)
You can think of a `for` loop as meaning `while there are remaining
elements to be iterated, keep cycling` and the `else` clause applies when
there are no more elements. A `break` statement does not trigger the else
clause because the iteration was not exhausted.
Once you get the idea, it's very simple.
It's useful when you want to search for an item and to do something if
you don't find it, eg:
for i in items:
if is_wanted(i):
print "Found it"
break
else:
print "Didn't find ir"
* Dustan (Fri, 19 Oct 2007 11:39:04 -0000)
On Oct 19, 3:12 am, Thorsten Kampe <thors...@thorstenkampe.dewrote:
So a for/else loop is exactly the same thing as a for loop with the
else clause outside the loop (except for "break")?
Am I missing something here? It sounds to me like you just described
two identical constructs.
#
for i in range(10):
print i
else:
print 'the end!'
#
is the same else
#
for i in range(10):
print i
print 'the end!'
#
MRAB schrieb:
On Oct 19, 4:11 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Thu, 18 Oct 2007 23:44:27 -0300, Ixiaus <parnel...@comcast.net> escribió:
>>I have just come across a site that discusses Python's 'for' and 'while' loops as having an (optional) 'else' structure. At first glance I interpreted it as being a bit like the 'default' structure in PHP's switch block... But the switch block isn't a loop, so, I am now confused as to the reason for using 'else' with the for and while loops... A few quick tests basically show that statements in the else structure are executed at the fulfillment of the loop's expression (ie, no break).
A `while` loop tests a condition: if it evaluates to true, keep cycling; if it is false, stop. The `else` clause is executed when the condition is false, as in any `if` statement. If you exit the loop by using `break`, the `else` part is not executed (because you didn't get out of the loop by determining the condition falseness)
You can think of a `for` loop as meaning `while there are remaining elements to be iterated, keep cycling` and the `else` clause applies when there are no more elements. A `break` statement does not trigger the else clause because the iteration was not exhausted.
Once you get the idea, it's very simple.
It's useful when you want to search for an item and to do something if
you don't find it, eg:
for i in items:
if is_wanted(i):
print "Found it"
break
else:
print "Didn't find ir"
Wrong. It's not:
for i in []:
print i
else:
print "I'm reached, too"
prints out "I'm reached, too"
The else will ONLY not get executed when the loop is left prematurely
through a break:
for i in [1]:
print i
break
else:
print "I'm reached, too"
won't print the "I'm ..."
Diez This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Xah Lee |
last post by:
# -*- coding: utf-8 -*-
# Python
# David Eppstein of the Geometry Junkyard fame gave this elegant
# version for returing all possible pairs from a range of n numbers.
def combo2(n):
return...
|
by: Sergej Andrejev |
last post by:
Not long ago I was asked to give PHP lections to some private IT
school. I'm on second lection now, and will be teaching my :) students
conditional statements (if..else and switch) and loops (while...
|
by: foker |
last post by:
I have an array with 50 elements in it, and a huge document with like
35,000 words on it. What I want to do is count the number of times each
element has appeared in the document. This is what I...
|
by: Holger |
last post by:
I needed a tool for extracting patches from CVS based on the log
messages. I.e. we mark our fixes and features with a "Bugdb XYZ"
And sometimes you need to move a fix/feature to another branch or...
|
by: mike_wilson1333 |
last post by:
I would like to generate every unique combination of numbers 1-5 in a 5
digit number and follow each combo with a newline. So i'm looking at
generating combinations such as: (12345) , (12235),...
|
by: Wijaya Edward |
last post by:
Can we make loops control in Python?
What I mean is that whether we can control
which loops to exit/skip at the given scope.
For example in Perl we can do something like:
OUT:
foreach my $s1...
|
by: Zach Heath |
last post by:
I'm just starting C and haven't done programming for a few
years...could you guys please take a look at this? Thanks for your
time!
I have an input file that looks like:
1.5 2.5 Bob, Joe...
|
by: Ixiaus |
last post by:
I have just come across a site that discusses Python's 'for' and
'while' loops as having an (optional) 'else' structure.
At first glance I interpreted it as being a bit like the 'default'...
|
by: John Salerno |
last post by:
I know it's popular and very handy, but I'm curious if there are purists
out there who think that using something like:
for x in range(10):
#do something 10 times
is unPythonic. The reason I...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |