1. Goto Examples?
- Posted by Jeremy Cowgar <jeremy at cowg?r.c?m>
May 12, 2008
-
Last edited May 13, 2008
In order to decide if goto should be added, maybe I should have asked a
different question. Does anyone have examples of where goto is better than
existing syntax? Maybe we should look at how goto will "simplify" our programming
life?
So, if you have an example that will *simply* life, except for keywords that we
have (exit, continue) and for case statements (I think case will become part of
4.0), please post them here. They can be fictious examples that you would want to
do in real life, or examples in other languages that you wrote or have seen,
etc... Just examples of how goto is good. I think we all know how goto is bad, no
need to post those examples here, please.
Thanks!
--
Jeremy Cowgar
http://jeremy.cowgar.com
2. Re: Goto Examples?
- Posted by Kat <KAT12 at ?oosah?.net>
May 12, 2008
-
Last edited May 13, 2008
Jeremy Cowgar wrote:
>
> In order to decide if goto should be added, maybe I should have asked a
> different
> question. Does anyone have examples of where goto is better than existing
> syntax?
> Maybe we should look at how goto will "simplify" our programming life?
>
> So, if you have an example that will *simply* life, except for keywords that
> we have (exit, continue) and for case statements (I think case will become
> part
> of 4.0), please post them here. They can be fictious examples that you would
> want to do in real life, or examples in other languages that you wrote or have
> seen, etc... Just examples of how goto is good. I think we all know how goto
> is bad, no need to post those examples here, please.
Well, that lets out all my examples.
Kat
3. Re: Goto Examples?
Jeremy Cowgar wrote:
>
> In order to decide if goto should be added, maybe I should have asked a
> different
> question. Does anyone have examples of where goto is better than existing
> syntax?
> Maybe we should look at how goto will "simplify" our programming life?
>
> So, if you have an example that will *simply* life, except for keywords that
> we have (exit, continue) and for case statements (I think case will become
> part
> of 4.0), please post them here. They can be fictious examples that you would
> want to do in real life, or examples in other languages that you wrote or have
> seen, etc... Just examples of how goto is good. I think we all know how goto
> is bad, no need to post those examples here, please.
>
> Thanks!
>
> --
> Jeremy Cowgar
> <a href="http://jeremy.cowgar.com">http://jeremy.cowgar.com</a>
IMHO, in some cases (possibly not very common), the use of 'goto' is better than
'exit', 'continue', .... Maybe it's the best solution.
I'm thinking about navigation by keys, typically in console mode, when you have,
for example, a list of parameters to be adjusted.
Without 'goto', a solution is to nest each parameter input in a while loop. Even
using 'continue' the code is not very elegant:
Example:
...
while 1 do
-- Show A
key = get_key()
if (key = BACK) then exit end if
...
while 1 do
-- Show B
key = get_key()
if (key = BACK) then exit end if
...
while 1 do
-- Show C
key = get_key()
if (key = BACK) then exit end if
...
end while
if (key = BACK) then continue end if
end while
if (key = BACK) then continue end if
end while
if (key = BACK) then continue end if
return
This solution with 'goto' is cleaner and more simple:
...
ENTRY_A:
-- Show A
key = get_key()
if (key = BACK) then return if
...
ENTRY_B:
-- Show B
key = get_key()
if (key = BACK) then goto ENTRY_A end if
...
ENTRY_C:
-- Show C
key = get_key()
if (key = BACK) then goto ENTRY_B end if
...
This is just an example of an elegant use of 'goto' typically used in
microcontroller firmwares.
- Fernando
4. Re: Goto Examples?
Fernando Bauer wrote:
I'm not 100% sure I understood the flow, but here is my goto-less attempt.
...
label AA
while 1 do
-- Show A
key = get_key()
if (key = BACK) then exit AA end if
...
label BB
while 1 do
-- Show B
key = get_key()
if (key = BACK) then next AA end if
...
label CC
while 1 do
-- Show C
key = get_key()
if (key = BACK) then next BB end if
...
end while
end while
end while
return
> This solution with 'goto' is cleaner and more simple:
I think your 'goto' version has a few 'while loops' omitted, no?
--
Derek Parnell
Melbourne, Australia
Skype name: derek.j.parnell
5. Re: Goto Examples?
Jeremy Cowgar wrote:
>Does anyone have examples of where goto is better than existing syntax?
I direct your attention to this 1974 paper by Donald Knuth.
http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf
--
Derek Parnell
Melbourne, Australia
Skype name: derek.j.parnell
> end for
> end while
>
> or, if you absolutely need a "label" statement
>
> while 1 do ~top
> for a = 1 to 10 do ~mid
> end for
> end while
I think for as much as it will be used and for clarity and parsing reasons,
label "abc" would be best, but others may disagree. It especially will not work
after the do. It would be hard to detect if it's the first execution token of the
block or a label. Also, I do not think the label would need to be quoted, it
would just be label top.
--
Jeremy Cowgar
http://jeremy.cowgar.com
6. Re: Goto Examples?
Derek Parnell wrote:
>
> Fernando Bauer wrote:
> I'm not 100% sure I understood the flow, but here is my goto-less attempt.
>
> ...
> label AA
> while 1 do
> -- Show A
> key = get_key()
> if (key = BACK) then exit AA end if
> ...
> label BB
> while 1 do
> -- Show B
> key = get_key()
> if (key = BACK) then next AA end if
> ...
> label CC
> while 1 do
> -- Show C
> key = get_key()
> if (key = BACK) then next BB end if
> ...
> end while
> end while
> end while
> return
Ok, thanks! It's better than my original version. When I wrote it I was only
considering the use of statements without labels (Eu3.2). However, the nested
while loops structure remains and IMHO it's not as elegant and simple as the
'goto' solution.
>
> > This solution with 'goto' is cleaner and more simple:
>
> I think your 'goto' version has a few 'while loops' omitted, no?
No. Could you clarify your point? I've been using that kind of structure in
several projects for many years.
>
> --
> Derek Parnell
> Melbourne, Australia
> Skype name: derek.j.parnell
Regards,
Fernando
P.S.: I'm not proposing the implementation of goto, I was just replying to a
question from Jeremy.