Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Absolute need of goto 4 correct functioning of loop routine.
#2
I'm going to go crazy with this GOTO argument. The fact that people have to try this hard to come up with an example of when GOTO makes sense to use--and then inevitably come up with the same example--speaks volumes to the utility of this command.

Proper code-planning negates the necessity for this command. GOTO is a logical nightmare to the program-flow; it sections off parts of your flowchart plan from the rest of it by interrupting the program flow and redirecting it to another part of code.

GOTO can be used by a careful coder in very specific instances to replace the logic that can be achieved by other commands whose purpose is to do exactly that, to save you a comparison or two in code segments that are unduly complex, such as a multiple-nested FOR loop with commands immediately following each NEXT statement, so that stacked NEXT isn't possible. Wow, that was a run-on sentence. :o

So if you write a complex bit of code like this, just to prove to people that GOTO is a "necessary evil", you can do it. But the code that you'll end up writing is something that is likely unnecessarily complex, and just invented to show off the use of GOTO.

Na_th_an brought up an interesting point in his last post on this subject: use of GOTO to optimize the speed inside a calculation-heavy loop. I do feel that in most instances, a nested loop structure built around use of GOTO at the exit condition would be better off scrapped, re-planned, and re-written. But then, I've not written many programs to the levels of complexity that Na_th_an has, so I think his opinions on this topic are likely more valid than my own.

This example, which I keep seeing:

[syntax="QBASIC"]
FOR x1% = 1 TO BigNumber1%
FOR x2% = 1 TO BigNumber2%
FOR x3% = 1 TO BigNumber3%
FOR x4% = 1 TO BigNumber4%
IF (exitcondition) THEN EXIT FOR
NEXT x4%
IF (exitcondition) THEN EXIT FOR
NEXT x3%
IF (exitcondition) THEN EXIT FOR
NEXT x2%
IF (exitcondition) THEN EXIT FOR
NEXT x1%
[/syntax]

can be simplified to:

[syntax="QBASIC"]
FOR x1% = 1 to BigNumber1%
FOR x2% = 1 to BigNumber2%
FOR x3% = 1 to BigNumber3%
FOR x4% = 1 to BigNumber4%
IF (exitcondition) THEN EXIT FOR
NEXT x4%, x3%, x2%, x1%
[/syntax]

You can further complicate the already-intimidating structure by throwing in commands between the NEXT list, which renders them unable to stack. In this case I either: 1. don't use FOR loops, 2. am already using SUBS to block off pieces of logically relevant code (as, at this point, the code is already being rather horrendous) which makes the whole argument a moot point, or 3. trip the exit condition by adjusting the comparison variables inside the inner-most loop. In fact, I typically prefer these methods to EXIT FOR itself.

Why? Because when you start a FOR loop, you are writing a looping structure that has a built-in exit condition. That's what the FOR loop is there for: To execute a piece of code an exact number of times. When I look at something that says:

Code:
FOR i% = 1 TO 10

I expect the loop to run 10 times, and for i% to equal 11 when it's done. If you're kicking out of the structure before this exit condition is met--either via GOTO, EXIT, etc--maybe you shouldn't be using a FOR...NEXT loop in the first place. Personally, I like to see the exit condition of a loop met to make it stop running, if at all possible.

In my experience, I've never encountered a situation where use of GOTO made the most sense. I've always been able to refactor the program in a more logical way which didn't necessitate use of GOTO.

I find this interesting reading: http://c2.com/cgi/wiki?GotoConsideredHarmful

That example looks familiar. >.>

*peace*

Meg.
Reply


Messages In This Thread
Absolute need of goto 4 correct functioning of loop routine. - by Meg - 02-02-2005, 07:19 PM
Need more - by Mac - 02-03-2005, 12:50 AM
Mea culpa - by Mac - 02-03-2005, 01:57 AM
Using goto - by Mango - 02-12-2005, 04:45 PM
Not required though - by Mac - 02-12-2005, 07:55 PM
Got UnLazy - by Mac - 02-12-2005, 11:12 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)