UseGOTO


When you walk into the qbasicnews forum and ask a question that has any faint connection to the command "GOTO", you will be perplexed at the result. What is the result? Well, it's a war.

First, what GOTO does: it jumps from one place in your program to another:

GOTO label1
...
...lots of code here....
...
label1: PRINT "You jumped"

GOTO (jump) is a basic concept of assembly language. Higher level constructs are created from combinations of GOTO.

Some say that using GOTO is bad and some say that it's good. In fact, this argument can go on for weeks at a time.

Um, so is it good or bad? Well, that's the same as asking whether chocolate is good or bad? (assuming that you are average and won't die from it or are not allergic to it)

So you have these two people, a chocolate connesieur (C), a dentist (D), and doctor (D2) arguing amongst you.


D1: It's bad, 'cuz it's bad for your teeth!

C: No, it's good, 'cuz it's delicious! How can you live without it?

D2: You'll die with it! Too much sugar!

C: You'll die without it! Too little sugar!

D1, D2: No!

C: Yes!

C: Eat or I stab you! *STAB*..

D1: You killed him!

C: *sigh* Is there a Doctor in the house?

D1: This would have never happened if you hadn't been promoting it!

C: This would have never happened if your irrationality didn't get to you!

And so on...

The solution to this perplexing dillemma of the century, gentlemen, is to eat a LITTLE chocolate, because it is good for you, and then BRUSH YOUR TEETH... *hmm.. where have we heard that before??* Problem solved!
An even better solution is to eat "chocolate" that isn't bad for your teeth OR your health! :)



Here is how it applies to GOTO:

GOTO's faults are that it may be hard to understand how your code works after you write it, and it may be much harder to debug. It's hard to figure out anything if the program jumps from one place to another. The logical flow can be difficult to debug. Sometimes, it's easier to use because another programming structure might prove useful.

It's usually not recommended for newbies but only for experts who know what they're doing. So how do the experts know what to do?
If you're a newbie to programming and want to learn more, you should experiment with using GOTO, and then try to experiment with other structures, such as FOR...NEXT, DO...LOOP, GOSUB...RETURN.. SUB...END SUB, FUNCTION...END FUNCTION. There are many viable alternatives to GOTO. After you learn other structures, you will be able to judge at whether using GOTO makes sense.

The catch about using other structures rather than GOTO is that other structures currently do not capture the full functionality of GOTO.

Many basic languages, including QBasic, do not have a "continue" command: the command that comes back to the beginning of any loop (FOR, DO..). It must be substituted for a label and GOTO. Only Powerbasic, as far as I know, (Jan. 2004) supports for instant multiple structure exits, like this:

DO
DO
DO
i = 3
IF i = 3 THEN EXIT, EXIT, EXIT 'or EXIT(3)
LOOP
LOOP
LOOP
(instead of)
DO
DO
DO
i = 3
IF i = 3 THEN EXIT DO
LOOP
IF i = 3 THEN EXIT DO
LOOP
IF i = 3 THEN EXIT DO
LOOP
(which can be simulated by)
DO
DO
DO
i = 3
IF i = 3 THEN GOTO exitAllLoops
LOOP
LOOP
LOOP
exitAllLoops:


So, in conclusion: use GOTO, but only when it is necessary. There are many structures that provide coders with much a better and coherent way of jumping... It's good for debugging, it's good for any fellow coders who might want a look at your code, it's good for yourself working on the same program one week later. Remember:

 GOTO = chocolate 


-Agamemnus


UseGOTO - page last edited 2004-01-19 05:34:20 by 172.131.255.43 (home) (edit)
Blast WIKI - by RoboticBoy - edited and tweaked for our evil purposes by Hexadecimal Disaster