Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Languages with GOTO

Name: suomynonA 2007-07-17 12:49 ID:sJhm9IHJ

Is there a modern programming language which has GOTO function in it? I know basic from a millenia ago and recently learned java, and well I hated OOP, but I could live with it if I had goto.

Name: Anonymous 2007-07-19 23:45 ID:8wGLwsNB

dealing with error situations that don't happen very often, in such a way that the error handling code doesn't clutter up the main code path.
in that case, the code reads like regular exceptions, except all in the same function:

if(error)
   goto error_handle

...

error_handle:

~~~
is similar if not almost equal to

if(error)
   throw exception_a

...

catch (exception_a)

Name: Anonymous 2007-07-19 23:52 ID:bKSfkdkf

>>41
its C we are talking about here
dealing with errors happens at every single fucking stdlib function call, idiot.

Name: Anonymous 2007-07-19 23:58 ID:0hO7/Wyv

>>41
lol OKAY. Call it throw if it makes you feel so much better.

Name: Anonymous 2007-07-20 1:39 ID:oyT1fTO7

why do c weenies try so hard to come up with some other way to do things that would be much simpler and cleaner if they'd just use another function?

Name: Anonymous 2007-07-20 2:28 ID:TGq5iOhg

>>36
Taken from: http://cpptips.hyperformix.com/cpptips/sometimes_goto.txt


TITLE: a concise example using "goto"

(Source: Private mailing from David on Mon Oct  6 11:24:58 1997)


TRIBBLE: David R. Tribble (tribble@central.beasys.com">david.tribble@central.beasys.com)

Sometimes using goto statements is the most concise and understandable
form for certain programming constructs.

For example:

    void parse()
    {
        Token* tok;

    read:
        tok = getToken();       // 1
    shift:
        if(shiftTok(tok))      // 2
            goto read;
        if(reduceTok(tok))     // 3
            goto shift;
    }

The three statements above form two tight intertwined loops.  It's left as an excercise for the reader to rewrite the loops without using gotos. But you'll find that the way it's written above is the most concise and clearest.

Name: Anonymous 2007-07-20 2:57 ID:iyxR1U81

Name: Anonymous 2007-07-20 3:54 ID:E5mRppHe

One word, the writing of state machines, thread over.

Name: Anonymous 2007-07-20 3:56 ID:3iXZjsvK

/usr/src/linux% grep -R goto drivers | wc
  17037   53116  790305

And 99% of them are good old gotos, not comments.

Name: Anonymous 2007-07-20 4:11 ID:oyT1fTO7

>>45
for(tok=getToken();reduceTok(tok);if(shiftTok(tok))tok=getToken());

Name: Anonymous 2007-07-20 4:12 ID:C8qdGGdG

>>40
Which is why non-worthless languagres are Wirthless.

Name: Anonymous 2007-07-20 8:16 ID:Heaven

>>49
You repeated tok=getToken(). Also, space your fucking code out.

Name: Anonymous 2007-07-20 10:28 ID:tNjyyvLd

You repeated tok=getToken().
what's wrong with that?

Also, space your fucking code out.
for(
 tok=getToken();
 reduceTok(tok);
 shiftTok(tok) && (tok=getToken())
);

Name: iTG 2007-07-20 11:42 ID:Heaven

>>51
DO YOU FUCKING KNOW ANY C AT ALL YOU FUCKING IDIOT?

for(a; b; c) { d; }
IS

a;
while(b) {
d;
c;
}


YOU FUCKING COCKSUCKER.

>>49,52
AWFUL INDENTATION, NO NEED FOR THESE PARENTHESES STUPID CODE ETC., GTFO.
FUCKING SAGE

Name: Anonymous 2007-07-20 11:50 ID:tNjyyvLd

>>53
indentation was for >>51, i wouldn't normally put it on more than one line like that.
and there are no unnecessary parentheses. && has higher precedence than =.

Name: Anonymous 2007-07-20 11:54 ID:Heaven

&& has higher precedence than =.
ORLY

Name: Anonymous 2007-07-20 12:14 ID:TGq5iOhg

>>49 and >>52
Wrong.  If shift returns true - causing read to be called - the code will call reduce before calling shift again.

Also
-short circuiting and casting pointer to bool is far less transparent than the goto version
-the if statement squeeze in the for loop isn't very readable either

Name: Anonymous 2007-07-20 13:19 ID:imyqVLsp

>>53

lol @ mental illness

Name: Anonymous 2007-07-20 13:33 ID:Heaven

Wrong.  If shift returns true - causing read to be called - the code will call reduce before calling shift again.
for( tok=getToken() ; shiftTok(tok) ? tok=getToken() : 1 || reduceTok(tok) ; );

-short circuiting and casting pointer to bool is far less transparent than the goto version
the "casting pointer to bool" is just nonsense. a bool in c99 is just an int. and the code in >>45 uses the pointer the same way.

Name: Anonymous 2007-07-20 13:49 ID:vc5pvD9l

>>11
THE TERM IS HAMFUL, IDIOT.

Name: Anonymous 2007-07-20 13:54 ID:oDKLzh6L

Name: Anonymous 2007-07-20 13:57 ID:Heaven

>>60
You're slow, blind and stupid. >>46

Name: NameFag !BJgNTPNob. 2007-07-20 18:19 ID:i1TutOZe

>>46
>>60
I looked for this thread only to post that, yet I was beaten twice...

Name: Anonymous 2007-07-20 22:47 ID:uVv7ONRU

Name: Anonymous 2007-07-21 0:49 ID:3FJdXgPe

I'd like to see you turn THREE interlaced loops with goto into equally readable (>>58 doesn't count), equally efficient code.

a:
 do1();
b:
 if(do2()) goto a;
c:
 if(do3()) goto b;
d:
 if(do4()) goto c;
 if(do5()) goto d;

Name: Anonymous 2007-07-21 1:33 ID:1dgEHm0o

>>64
It is not possible to write this code readably because it has no function.
The code just does 1 2 3 4 or 5 and jumps around.
This is not somthing which can possibly ever occur in real programming because you have an actual task to solve or algorithm to implement, so things are named properly and are done for a purpose.

MY CONCLUSION, YOUR EXAMPLE SUCKS

Name: Anonymous 2007-07-21 1:53 ID:+IScw2W8

>>64

That can be done easily enough with a simple FSM. It isn't exactly "equally efficient" as your code, but is a great deal more readable (at least to me). Here, the process is broken up into discrete steps which are separate from each other. It is easier to think about the process in terms of how to organize the steps in relation to each other, without having to mess about with what line of code each one is on.


char state = 'a';

int stopped = 0;

while(!stopped)
{
    switch(state)
    {
        case 'a':

            do1();

            state = 'b';

            break;

        case 'b':

            if(do2())
            {
                state = 'a';
               
            } else {
               
                state = 'c';
               
            }
            break;

        case 'c':

            if(do3())
            {
                state = 'b';
               
            } else {
               
                state = 'd';
               
            }
            break;

        case 'd':

            if(do4())
            {
                state = 'c';

            } else {
               
                state = 'e';
           
            }
            break;

        case 'e':
           
            if(do5())
            {
                state = 'd';
               
            } else {
               
                // quit
               
                stopped = 1;
               
            }
            break;
       
    }
}


Name: Anonymous 2007-07-21 2:01 ID:+IScw2W8

>>66

And here's the same FSM but slightly less verbose. I've removed the "else" statements and allowed the cases to fall through each other. This lowers the readability, but uses less code. The readability now becomes more like your original example in >>64 while still requiring no goto statements.


char state = 'a';

int stopped = 0;

while(!stopped)
{
    switch(state)
    {
        case 'a':

            do1();

        case 'b':

            if(do2())
            {
                state = 'a';
                break;
            }
               
        case 'c':

            if(do3())
            {
                state = 'b';
                break;
            }

        case 'd':

            if(do4())
            {
                state = 'c';
                break;
            }

        case 'e':
           
            if(do5())
            {
                state = 'd';
                break;
            }
           
        default:
               
            // quit
           
            stopped = 1;
               

    }
}


Name: Anonymous 2007-07-21 10:47 ID:NmAOqu8l

>>67
K&R, are you cool enough to use it?

Name: Anonymous 2007-07-21 17:46 ID:bsbvlkzb

>>66
That can be done easily enough with a simple FSM.
Flying Spaghetti Monster can do that? Wow!

Name: Anonymous 2007-07-21 17:56 ID:Z12cYxEi

>>69
Finite State Machine

Name: Anonymous 2007-07-21 18:41 ID:Heaven

Fucking Spaghetti-code Monster

Name: Anonymous 2007-07-21 19:16 ID:MbGbf096

>>42
>>43
lol, you missed the point. I just said it read equally, I made no other assertions.
>>66
finite state machines are as unreadable as goto.

>>45
void read()
{
    shift(getToken());
}
void shift(Token* tok)
{
    if(shiftTok(tok))
       read();
    else if(reduceTok(tok))
       shift(tok);
}

(notice parse = read in here)
but don't blame me for your compiler's lack of tail call optimization.

Name: Anonymous 2007-07-21 20:18 ID:MbGbf096

>>64
ooh, I forgot

a:
 do1();
b:
 if(do2()) goto a;
c:
 if(do3()) goto b;
d:
 if(do4()) goto c;
 if(do5()) goto d;

becomes
void a()
{
   do1();
   b();
}
void b()
{
   if(do2()) a();
   else b();
}
void c()
{
   if(do3()) b();
   else d();
}
void d()
{
   if(do4()) c();
   else if(do5()) d();
}

Name: Anonymous 2007-07-21 22:51 ID:+IScw2W8

>>73

void b()
{
   if(do2()) a();
   else b();
}

else b() ??? Think you mean else c(). Otherwise, c(), and therefore d() never get called, and your program never terminates if we begin at a(). Though, I think you just typo'd.

Still, ignoring the error, it is functionally equivalent to >>64 , >>66 , and >>67

It would then be a matter of how the code can be optimized when compiling. Other than that it is just a matter of style and there isn't much of an argument either way.

Name: Anonymous 2007-07-22 4:08 ID:61PMylaX

>>73
PROTIP: do1(),do2(),... in most cases are note functions, but blocks of code, using same variables. Good job doing recursion, but it's too much work to send them all to your a(),b(),...

Name: Anonymous 2007-07-22 4:30 ID:NI4VFhBu

>>75
ONE WORD, CLOSURES, THREAD OVER.

Name: Anonymous 2007-07-22 13:46 ID:Rb/irdUC

int doLotsOfStuff()
{
    int err;

    if (err = doOne()) goto oneFail;
    if (err = doTwo()) goto twoFail;
    if (err = doThree()) goto threeFail;

    return 0;

threeFail:
    undoTwo();

twoFail:
    undoOne();

oneFail:
    return err;
}

Yeah, sorry but you can pry my goto from my cold, dead fingers. Exceptions + RAII are a decent substitute for this, but if you're working in plain old C (and for a lot of small to medium scale applications C++ just adds too much baggage), the above idiom works quite nicely for me, ta.

Name: Anonymous 2007-07-22 13:52 ID:AGgx7XQ2

goto makes your computer explode, don't type it. OH SHI-

Name: Anonymous 2007-07-22 14:15 ID:Heaven

goto makes raptors attack you.

Name: Anonymous 2007-07-22 14:20 ID:zPhrPQaj

if you're working in plain old C the above idiom works quite nicely for me, ta.
of course. goto is a must in C. but not in other languages.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List