'while/else' (ugh) (was Re: [oclug] Ruby)
Francis J. A. Pinteric
linuxdoctor at linux.ca
Fri Jun 27 14:46:54 EDT 2003
On Fri, 27 Jun 2003 14:19:46 -0400 (EDT)
"David F. Skoll" <dfs at roaringpenguin.com> wrote:
> On Fri, 27 Jun 2003, Rod Giffin wrote:
>
> > It appears you're right, and it's not what I'm looking for at all.
> > Because what I'm looking for is a way to reduce the structure of
> > decision trees in while loops... for example, if the "else" ran only
> > if the while condition generates a false when it attempts to run.
>
> You can reduce the nesting as follows:
>
> rs = select id from test where condition = true
> if rs == EMPTY
> status.value = "no records found"
> endif
> while rs <> EMPTY
> blah....
> endwhile
>
> Purists may be offended by the redundant test, but it's a tradeoff
> between the redundant test and an extra level of nesting.
Purists would replace the `endif' with an else.
The purpose of an `elsewhile' is to fire when the loop completes so as
to perform any cleanup that may be necessary as a result of side effects
from the loop. For instance, if the loop allocated memory blocks
temporarily that would need to be freed at the end of the loop.
Obviously, if the loop never executed such actions would not be
required.
So, your above code could be written:
if rs == EMPTY
status.value = "no records found"
else
while rs <> EMPTY
blah with memory allocation ...
elsewhile
cleanup blah ...
end
Of course some other Purists might prefer:
if rs == EMPTY
status.value = "no records found"
else do
while rs <> EMPTY
blah with memory allocation ...
endwhile
cleanup blah ...
end
To each his own.
>>>--fja->
--
Fashion is for people who have no style.
There are three crimes which deserves the death penalty:
conformity, political correctness, and smoking.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://tux.oclug.on.ca/pipermail/oclug/attachments/20030627/6b4edf62/attachment.bin
More information about the OCLUG
mailing list