Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 08, 2002 while else? | ||||
---|---|---|---|---|
| ||||
One feature I've seen in a couple of languages that I like is a finally clause for looping constructs such as 'while' and 'for'. Example: while(expression) { } finally { } The finally clause is executed if the loop terminates normally when the expression evaluates to false. If the loop is exited via a break statement the finally clause is not executed. The most common place I have seen this come in handy are loops that are doing a search and you want to do something if the search fails. for (i = 0; i < a.length; i++) { if(a[i] == search_value) break; } finally { // Couldn't find search value so add it a ~= search_value; } Patrick Down |
March 23, 2003 Re: while else? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | "Patrick Down" <pdown@austin.rr.com> escribió en el mensaje news:a1e2b8$26eb$1@digitaldaemon.com... | | One feature I've seen in a couple of languages that I | like is a finally clause for looping constructs such as | 'while' and 'for'. Example: | | while(expression) | { | } | finally | { | } | | The finally clause is executed if the loop terminates normally | when the expression evaluates to false. If the loop is exited | via a break statement the finally clause is not executed. The | most common place I have seen this come in handy are loops | that are doing a search and you want to do something if the | search fails. | | for (i = 0; i < a.length; i++) | { | if(a[i] == search_value) | break; | } | finally | { | // Couldn't find search value so add it | a ~= search_value; | } | | | Patrick Down | | No one answered to this, either. I think it's interesting. Similar to the do { ... } while (...) { ... } proposed some time ago. Maybe the finally keyword could be changed. Thoughts? ————————————————————————— Carlos Santander --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.463 / Virus Database: 262 - Release Date: 2003-03-17 |
March 24, 2003 Re: while else? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | "Carlos Santander B." <carlos8294@msn.com> wrote in news:b5keer$1570$1@digitaldaemon.com: > > No one answered to this, either. I think it's interesting. Similar to the do { ... } while (...) { ... } proposed some time ago. Maybe the finally keyword could be changed. Thoughts? Python uses "while else" and "for else" for the same thing. I thought "finally" made more sense. |
September 02, 2009 Re: while else? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | == Quote from Patrick Down (pat@codemoon.com)'s article
> "Carlos Santander B." <carlos8294@msn.com> wrote in news:b5keer$1570$1@digitaldaemon.com:
> >
> > No one answered to this, either. I think it's interesting. Similar to the do { ... } while (...) { ... } proposed some time ago. Maybe the finally keyword could be changed. Thoughts?
> Python uses "while else" and "for else" for the same thing. I thought "finally" made more sense.
Also it would be nice to add a "while-else" statement like that:
while (condition1)
{
xxx
}
else
{
yyy
}
that will do the same thing as below:
if (condition1)
{
do
{
xxx
}
while (condition1);
}
else
{
yyy
}
or
bool b = false;
while (condition1)
{
b = true;
xxx
}
if (!b)
{
yyy
}
It will be usefull to do such thing like fetching rows from db. When there are no rows you can send a message using ele.
Also it may be applied to for and foreach statements.
|
Copyright © 1999-2021 by the D Language Foundation