Re: Goto Examples?

new topic     » goto parent     » topic index » view thread      » older message » newer message

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

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu