Re: GOTO
G'day all
I get my postings via digest, and haven't followed this thread word for
word, so if I'm repeating someone else's ideas... oops, sorry :(
Using the following as an example:
while 1 do
while 1 do
if condition then
doExit = true -- set flag
exit
end if
end while
-- we now have to check this flag "every time"
if doExit = true then
exit
end if
-- more code
end while
can lead to some ugly code (especially when the doExit flag is a long way
from the interior end while, or where lots of conditions are being tested,
etc. What I propose is a new "until" statement, in the form
until <condition> then
-- code
end until
The code inside the "until" block would execute until <condition> becomes
true. Euphoria could detect that <condition> had become true at any point
in the code, and "pretend" that the programmer had coded an "if..exit..end
if" thing. For example:
until doExit = true do
while 1 do
if condition then
doExit = true -- triggers immediate exit from "until" block
end if
end while
-- more code (run only if doExit is still false)
end until
This change to the language would result in nice neat code, and wouldn't
break any existing stuff out there. Of course, it should be possible to
use a normal "exit" statement somewhere inside the "until" block, eg:
until doExit = true do
while 1 do
if condition1 then
doExit = true -- triggers immediate exit from "until" block
end if
-- some more code (run only if doExit is still false)
if condition2 then
exit -- only exit from this "while" block
end if
for i = 1 to 20 do
if condition3 then
doExit = true -- triggers immediate exit from "until" block
end if
-- some more code (run only if doExit is still false)
if condition4 then
exit
end if
next
end while
-- more code (run only if "exit" used inside "while" loop)
end until
The "until" statement should be able to handle multiple flags by a method
similar to the next example. This example shows a method for determining
where in the "until" block the exit came from, as well as an example of
exiting a for loop:
until or_all(doExit1, doExit2) = true do
while 1 do
if condition1 then
doExit1 = true -- triggers immediate exit from "until" block
end if
-- some more code (run only if doExit is still false)
if condition2 then
exit -- only exit from this "while" block
end if
for i = 1 to 20 do
if condition3 then
doExit2 = true -- triggers immediate exit from "until" block
end if
-- some more code (run only if doExit is still false)
if condition4 then
exit
end if
next
end while
-- more code (run only if "exit" used inside "while" loop)
end until
if doExit1 = true then
-- oopscode #1
elsif doExit2 = true then
-- oopscode #2
end if
This assumes that a "goto"-like thing would only be used for exiting loops,
and not implemented in a "goto label" style generic goto. Personally, I've
maintained enough spaghetti code already in this lifetime, and I'd hate to
see Euspaghetti!
Regards
Tony
|
Not Categorized, Please Help
|
|