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

Pages: 1-4041-8081-

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-17 13:19 ID:OLxRAl5o

If you want to be succesful in programing, DONT USE GOTO
It makes hard to manage spigetti code.  Just learn a real language. and no, java dosnt have goto.

Name: Anonymous 2007-07-17 13:20 ID:Hlzkdp1E

You can use goto in Python. It's considered very pythonic and is a recommended best practice.

http://entrian.com/goto/

Name: Anonymous 2007-07-17 13:55 ID:iAfpsAX4

C++ has goto.

Name: Anonymous 2007-07-17 13:55 ID:+EkgJc4r

Most modern programming languages have goto, they just spell it T-H-R-O-W.

Name: Anonymous 2007-07-17 14:34 ID:7UcD/Lgu

>>5
Lol Truth.

Name: Anonymous 2007-07-17 14:44 ID:7SUedsqK

>>1
Goto is useful, but not for what you want to do.

java, and well I hated OOP
Of course. Java has the poorest object system I know.

I could live with it if I had goto.
WTF, what would you do with it?

If you want to be succesful in programing, DONT USE GOTO
Except if you use it in the form of tail calls, exceptions, or break/continue, for example.

>>3
Witty.

Name: Anonymous 2007-07-17 16:00 ID:4hlbAvg8

PHP 6 has throw and goto!

Name: Anonymous 2007-07-17 16:27 ID:2+WK9M+7

>>2
It's even harder to read deeply indented code.

Java has goto, it's called break with a label.

Name: Anonymous 2007-07-17 16:35 ID:7SUedsqK

>>9
Don't tell him. Break with a label is one of the features that are useful, but not in the cases most programmers will want to use them. I'd allow break with label and goto, but in order to use them you'd have to declare:

#define _EXPERT_PROGRAMMER_

Name: Anonymous 2007-07-17 16:45 ID:qDjd3K0y

GOTO CONSIDERED DANGEROUS

Name: Anonymous 2007-07-17 16:52 ID:jg6dahoJ

>>7
Except if you use it in the form of tail calls, exceptions, or break/continue, for example.

thats not GOTO

Name: Anonymous 2007-07-17 16:54 ID:7SUedsqK

99.9% of the times goto has been used in the last 15 years, it has been misused. As for the 0.1% of the times where it was actually a good idea, none of the programmers who like to use goto would understand why.

In the past 4 years working in this business, I found goto useful for two times.

Name: Anonymous 2007-07-17 17:45 ID:Heaven

int main(){
 int n=arc4random();
 return a(n);
}

int a(int n){ return b(n-1); }
int b(int n){ return n?a(n):n; }

Name: Anonymous 2007-07-17 18:36 ID:UU5Eg94T

>>13
Only four years? You've got a lot to learn, junior.

Name: Anonymous 2007-07-17 19:08 ID:7SUedsqK

>>15
Professionally.

Name: Anonymous 2007-07-18 6:58 ID:lvf9nY4G

Name: Anonymous 2007-07-18 7:52 ID:WkIT+GGo

>>13
Agree. I've only used goto about 3 times in C++, two of those were out of pure laziness and the other one was an optimisation for speed critical code. The alternative for the third one would have been much worse.

Name: Anonymous 2007-07-18 19:14 ID:FkSXquQU

common lisp is what you want. A real, flexible object system, imperative and functional programming, logic, and macros. And it has goto!

Name: Anonymous 2007-07-18 19:22 ID:+9nzGV6v

>>14

(lambda (lambda x (lambda x (lambda x x) (lambda x (lambda (lambda x x) (lambda (lambda x (lambda x x) x) ) (lambda x x) lambda x) x )))

Does the same but is EXPERT PROGRAMMATION and is O(ln(ln(ln(ln(ln(ln(x ln(x)))))))).

True EXPERT PROGRAMMERS who've read SICP know that goto is unneccesary to construct an arbitrary program.

Name: Anonymous 2007-07-19 3:19 ID:eQovlAaX

>>20
I AM A CHAMPION PROGRAMMER, I HAVE READ K&R BECAUSE SICP IS A BEGINNER BOOK

Name: Anonymous 2007-07-19 4:58 ID:UjVITlR4

Any language with an inline assembler will allow you.  Just chuck in a label and a JMP instruction.  Then spend your life debugging.

Name: Anonymous 2007-07-19 5:14 ID:VK3c3dVw

Lambda, the ultimate goto

Name: Anonymous 2007-07-19 7:17 ID:QBsD/4JS

>>21
NO I DON'T GET THE IDEA BECAUSE YOUR CODE IS POORLY FORMATTED AND POOL-STILL-AIDS IS NOT USED, YOU DIDN'T TEST YOUR CODE, YOU ARE NOT AN EXPERT PROGRAMMER, YOU CAN'T USE IRC WELL, YOU CAN'T TAB COMPLETE, AND YOU CAN'T GOOGLE WHICH MEANS YOU'RE PROBABLY TOO BUSY JERKING OFF OR PLAYING VIDEO GAMES, DIE WELL, KID.

Name: Anonymous 2007-07-19 11:47 ID:84V+rEu/

Problem:
You have a function that does something, and in some cases it fails to do it, and there are some actions that need to be done after it failed.
I don't want to copy/paste code many times, and writing another function that handles the situation is out of question (writing #defines, too).
Solution: gotos. And i don't see any other solutions (in c, at least, and c++'s fancy throw/catch are the same thing as >>5 said). This is used alot in linux kernel.

Name: Anonymous 2007-07-19 12:16 ID:Heaven

>>25
int failed=do_something();
if(failed){
 do_some_shit();
 do_more_shit();
}

Name: Anonymous 2007-07-19 14:13 ID:84V+rEu/

>>26
you mean this, yes?

int a(void){
int failed=do_something();
if(failed){
 do_some_shit();
 do_more_shit();
}
return 0;
}

int b(void){
int failed=do_something();
if(failed){
 do_some_shit();
 do_more_shit();
}
return 1;
}

int b(void){
int failed=do_something();
if(failed){
 do_some_shit();
 do_more_shit();
}
return 2;
}

great solution, really.

Name: Anonymous 2007-07-19 14:39 ID:QBsD/4JS

goto is good for freeing things up in C on error.

int alloc_some_things()
{
  T *foo = malloc(sizeof(*foo));
  if (foo != NULL)
  {
     foo->member = malloc(sizeof(*foo->member));
     if (foo->member != NULL)
     {
         foo->another = malloc(sizeof(*foo->another));
         if (foo->another != NULL)
         {
             return foo;
         } else goto free_member;
     }  else goto free_foo;
  } else goto failure;
  free_member: free(foo->member);
  free_foo: free(foo);

  failure:
  return NULL;
}

Name: Anonymous 2007-07-19 14:41 ID:QBsD/4JS

>>28
Although that *does* depend on how you want to deal with it. You could of course just use assert(foo = malloc(sizeof *foo)); and that will abort() on failure, but you probably don't want to do that in a library.

Name: Anonymous 2007-07-19 16:03 ID:bKSfkdkf

>>28
LOL, very well put.

Name: Anonymous 2007-07-19 16:11 ID:bKSfkdkf

>>28

1) Learn C
2) Learn to write functions
3) Learn that GOTO is fucking useless

heres a better version of that code which doesn't use goto.
its shorter and easier to understand and modify.
You are a dumb fuck.


int alloc_some_things() { // this should be T but other poster is a fucking goto using idiot.
  T * foo;
 
  foo = malloc(sizeof(*foo));
  if(foo) {
    foo->member = malloc(sizeof(*foo->member));
    if(foo->member) {
      foo->another = malloc(sizeof(*foo->another));
      if(foo->another) {
    return foo; // THANK GOD WE ALLOCATED STUFF
      }
      free(foo->member);
    }
    free(foo);
  }
 
  return NULL; // learn C you fucking idiot.
}

Name: Anonymous 2007-07-19 19:53 ID:8wGLwsNB

int alloc_some_things() {
  T* foo;
  void* member;
  void* another;
  foo = malloc(sizeof(*foo));
  member = malloc(sizeof(*foo->member));
  another = malloc(sizeof(*foo->another);
  if(foo && member && another)
  {
     foo->member = member;
     foo->another = another;
  }
  else
  {
     free(foo); // free is ok on NULL
     free(member); // free is ok on NULL
     free(another); // free is ok on NULL
  }
  return NULL;
}

or, if you like some wild macro action:

#define RETURNFREE(val) freevec(autofree); return val

int alloc_some_things() {
  T* foo;
  void* member;
  void* another;
  void* autofree[] = {foo, member, another};
  foo = malloc(sizeof(*foo));
  member = malloc(sizeof(*foo->member));
  another = malloc(sizeof(*foo->another);
  if(BIG NESTED STUFF)
  {
........ ok ? then return
  }
  else
....
  {
    RETURNFREE
  }
  another big nesst.....

   RETURNFREE! AGAIN YOU DONT HAVE TO REWRITE THE CODE
  return NULL;
}

NOTE: I don't guarantee compilation on these code examples.
NOTE2: you can do this better with nested procedures, which the gnu extensions to C allow.

Name: Anonymous 2007-07-19 20:07 ID:bKSfkdkf

>>32
your first codepaste is an OK idea but I personally hate this style for the obvious reasons, in particular its not semantically equivalent to the original faggot.

Name: Anonymous 2007-07-19 20:38 ID:QBsD/4JS

>>31
sometimes it is not as simple as that, which is where goto has use. clearly my example did not communicate that to you. shrug

Name: Anonymous 2007-07-19 20:43 ID:QBsD/4JS

the funny thing is I never actually use goto and write it like >>31, but goto has its use where it is the least horrible thing to use for a given problem, like everything else. that's all I have left to say

Name: Anonymous 2007-07-19 22:39 ID:bKSfkdkf

>>34
that what ALL goto using faggots say to me, none of them can EVER produce an actual example of where goto is better.
MY CONCLUSION: GOTO FUCKING SUCKS DONT USE IT.

Name: Anonymous 2007-07-19 23:09 ID:Heaven

>>27
no.

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

klibc, vsnprintf.c:

                switch (ch) {
                case 'P':    /* Upper case pointer */
                    flags |= FL_UPPER;
                    /* fall through */
                case 'p':    /* Pointer */
                    base = 16;
                    prec = (CHAR_BIT*sizeof(void *)+3)/4;
                    flags |= FL_HASH;
                    val = (uintmax_t)(uintptr_t)
                        va_arg(ap, void *);
                    goto is_integer;

                case 'd':    /* Signed decimal output */
                case 'i':
                    base = 10;
                    flags |= FL_SIGNED;

[...]

                    /* fall through */
                case 'x':    /* Hexadecimal */
                    base = 16;
                    goto is_unsigned;

                is_unsigned:
                    switch (rank) {
                    case rank_char:
                        val = (uintmax_t)
                            (unsigned char)
                            va_arg(ap, unsigned
                                   int);
                        break;

Name: Anonymous 2007-07-19 23:21 ID:QBsD/4JS

>>36
argumentum ad logicam etc

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

Also read this: http://kerneltrap.org/node/553/2131

tl;dr:
>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.

>No, you've been brainwashed by CS people who thought that Niklaus Wirth actually knew what he was talking about. He didn't. He doesn't have a frigging clue.

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.

Name: Anonymous 2007-07-22 15:06 ID:JYsLKEDO

scheme

Name: Anonymous 2007-07-22 22:29 ID:B83mc77U

>>79
I liked that comic, too.

Name: Anonymous 2007-07-22 22:44 ID:Heaven

perl has goto...

Name: Anonymous 2007-07-22 23:30 ID:ckTBGamE

>>83

stfu perl

Name: Anonymous 2007-07-23 0:00 ID:NQEZsPQs

perl perl perl i love perl stop bashing

Name: Anonymous 2010-11-27 21:05

Name: Anonymous 2011-01-31 20:18

<-- check em dubz

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