www.digitalmars.com         C & C++   DMDScript  

D - while else?

reply "Patrick Down" <pdown austin.rr.com> writes:
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
Jan 07 2002
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"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
Mar 23 2003
parent reply Patrick Down <pat codemoon.com> writes:
"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.
Mar 23 2003
parent Ibrahim YANIKLAR <yanikibo gmail.com> writes:
== 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.
Sep 01 2009