Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does GOTO do?
#21
Hey guys,

Here's an example of a sort algorithm by Agamemnus which uses 5 precise goto's in the implementation of one of the fastest sort algorithms ever developed.
Code:
SUB AgaSort (amax AS INTEGER)

DIM g2(1 TO 32) AS INTEGER, h2(1 TO 32) AS INTEGER
DIM i AS INTEGER, j AS INTEGER, r AS INTEGER
DIM e AS INTEGER, g AS INTEGER, h AS INTEGER
REM DIM k AS INTEGER
DIM k AS INTEGER

e = 1
g2(1) = 1
h2(1) = amax
f1: g = g2(e): h = h2(e)
f2: i = g: j = h: r = (g + h) \ 2: k = Sortarray(r).length
f3: IF Sortarray(i).length < k THEN i = i + 1: GOTO f3
f4: IF Sortarray(j).length > k THEN j = j - 1: GOTO f4

IF i <= j THEN
IF i < j THEN SWAP Sortarray(i), Sortarray(j): SwapBars i, j
i = i + 1: j = j - 1
IF i <= j GOTO f3
END IF

IF j - g + i < h THEN
IF i < h THEN g2(e) = i: h2(e) = h: e = e + 1
h = j
ELSE
IF g < j THEN g2(e) = g: h2(e) = j: e = e + 1
g = i
END IF

IF g < h THEN GOTO f2
e = e - 1
IF e THEN GOTO f1
ERASE g2, h2
END SUB
You can probably remove the goto's and structure this code, but you will surely slow it down.
*****
Reply
#22
I never ever use GOTOs...
There's just no need for them.., it will make your program into a big heap of spaghetti...hehe Tongue

Just my oppinion.
<edit> But I suppose if you use them correctly theres nothing wrong with them, like in the sorting algorithm above.
It's the difference between asking someone how much flour goes into pancakes, and handing them a sorry mix of oozing green goo and asking them to fix it." - Deleter

-Founder & President of the No More Religion Threads movement-
Reply
#23
I think Aga states it very nicely, here:

http://faq.qbasicnews.com/?blast=UseGOTO

However, for you lazy guys that don't want to go there, here it is, and I hope Aga doesn't mind:

==============================================

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 ©, a dentist (D1), 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! Smile



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 a much 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
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#24
:lol: Just about spit out my drink as I was reading that... very nice and tastefully humorous explanation. Smile
Reply
#25
lmao

"Eat or I stab you! *STAB*"
"..You killed him! "

haha that is classic
Reply
#26
GOTO can be both good and bad. Generally, it develops bad habits and terrible code. But if you use it right, it can be extremely powerful. It's kinda like playing with assembly. Use it right, and you get something fast and cool (GOTO is faster than conventional loops). Use it wrong, and you screw up your program/computer.
.14159265358979323846264338327950288419716939937510582709445
Glarplesnarkleflibbertygibbertygarbethparkentalelelangathaffendoinkadonkeydingdonkaspamahedron.
Reply
#27
IMHO goto sucks :barf:

It makes the sourcecode hard to read and understand. But it's a good way for beginners to solve their problems with loops, "Y/N" questions and so on :wink:
Reply
#28
ive written functions that basically use a goto... that is theyll run some loops and leave after a certain condition .. so it'll just Return the value at that point.

using goto at this point in my programming practice seems absurd, and more likely to kill me in the end, especially when working on even a medium-sized project.
Reply
#29
Quote:You can probably remove the goto's and structure this code, but you will surely slow it down

This simply isn't true. Try the following code:

Code:
SUB AgaSort (amax AS INTEGER)

    DIM g2(1 TO 32) AS INTEGER, h2(1 TO 32) AS INTEGER
    DIM i AS INTEGER, j AS INTEGER, r AS INTEGER
    DIM e AS INTEGER, g AS INTEGER, h AS INTEGER
    REM DIM k AS INTEGER
    DIM k AS INTEGER

    e = 1
    g2(1) = 1
    h2(1) = amax

    DO
        g = g2(e): h = h2(e)

        DO
            i = g: j = h: r = (g + h) \ 2: k = Sortarray(r).length

            DO
                WHILE Sortarray(i).length < k
                    i = i + 1
                WEND

                WHILE Sortarray(j).length > k
                    j = j - 1
                WEND

                IF i > j THEN EXIT DO

                IF i < j THEN SWAP Sortarray(i), Sortarray(j): SwapBars i, j
                i = i + 1: j = j - 1
            LOOP WHiLE i<=j

            IF j - g + i < h THEN
                IF i < h THEN g2(e) = i: h2(e) = h: e = e + 1
                h = j
            ELSE
                IF g < j THEN g2(e) = g: h2(e) = j: e = e + 1
                g = i
            END IF

        LOOP WHILE g < h

        e = e - 1
    LOOP WHILE e

    ERASE g2, h2

END SUB

This should produce almost the same binary code as your source ... but it's structure is much easier to see. The reason for this is that a IF with a GOTO is almost the same as a WHILE/DO LOOP ...

EDIT: However, there are situations where GOTO's are really useful like:

  1. Exiting from multiple nested loops of the same kind (see the EXIT,EXIT,EXIT example above)
  2. The 1.5-loop problem[/list:o]

    But when you use GOTO's (or GOSUB's) then you should never forget to write a good explaining comment why you used this GOTO.

    Regards,
    Mark
Reply
#30
now that we have fb, maybe a continue keyword could be implemented so goto "hacks" don't have to be used for that case Big Grin :wink:
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side
...phear
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)