Re: Return from nested subroutines (still goto discussion)
- Posted by "Juergen Luethje" <j.lue at gmx.de> Nov 12, 2004
- 433 views
aku saya wrote: <snip> > Thank you, but... > > Maybe my code was too much simplified. What I meant is algorithms such > as backtracking or permutation. Now modified example: > > object d > d = {0,0,0,0,0} > > procedure r(integer x, integer y) > d[x] = y > if x = 5 then > if d[1]+d[2]+d[3]+d[4]+d[5] = 10 then > puts(1, "success") > -- (print d here) > goto ok > end if > else > for i = 0 to 3 do > r(x+1, i) > end for > end if > end procedure > > procedure main() > for i = 0 to 3 do > r(1, i) > end for > ok: > puts(1, "program end") > end procedure > > > Now the purpose of the program is to search d so that sum(d) = 10 and > every element of d is from 0 to 3. But when a solution is found, it > will just print the first solution found and continue to printing > "program end". > > Now how can it modified to use no GOTO and no decrease on performance? > (using flags to indicate whether a solution has been found would > decrease performance, I think) If I'm right, performance is not the main question here. Using GOTO this way, i.e. jumping out of procedures will corrupt the stack, no? So this should not be done anyhow, should it? Please correct me if I'm wrong. Regards, Juergen