goto

Discussion in 'Python' started by Hayri ERDENER, Jul 18, 2005.

  1. hi,
    what is the equivalent of C languages' goto statement in python?
    best regards
    Hayri ERDENER, Jul 18, 2005
    #1
    1. Advertising

  2. Hayri ERDENER

    Comcast Guest

    In python there is no goto statement. In C I use goto only in one case: to
    exit more then one level of blocks (as a matter of fact, I always use goto
    EXIT in C, where EXIT is the label of the end of the function).

    In python you can mimic this by throwing an exception and catching it.
    Exception should "know" the destination label name and the catch statement
    should compare this name with its (catch statement) name.


    "Hayri ERDENER" <> wrote in message
    news:...
    hi,
    what is the equivalent of C languages' goto statement in python?
    best regards
    Comcast, Jul 18, 2005
    #2
    1. Advertising

  3. Hayri ERDENER

    Peter Hansen Guest

    Hayri ERDENER wrote:
    > what is the equivalent of C languages' goto statement in python?


    Steven offered the best reply here, in that he wondered what you
    actually need this for. What usage of "goto" in C are you hoping to
    emulate? It's a certainty that some other non-goto technique will be
    more appropriate in Python.

    -Peter
    Peter Hansen, Jul 18, 2005
    #3
  4. Hayri ERDENER

    Kay Schluehr Guest

    Hayri ERDENER schrieb:
    > hi,
    > what is the equivalent of C languages' goto statement in python?
    > best regards


    No, but some of goto's use cases can be covered by unconditional jumps
    provided by exceptions.


    Here is a C function using goto:

    void main()
    {
    int i, j;

    for ( i = 0; i < 10; i++ )
    {
    printf( "Outer loop executing. i = %d\n", i );
    for ( j = 0; j < 2; j++ )
    {
    printf( " Inner loop executing. j = %d\n", j );
    if ( i == 3 )
    goto stop;
    }
    }
    /* This message does not print: */
    printf( "Loop exited. i = %d\n", i );
    stop: printf( "Jumped to stop. i = %d\n", i );
    }


    And here is a Python equivalent using exception handling:

    def main():
    class stop(Exception):pass
    try:
    for i in range(10):
    print "Outer loop executing. i = %d"%i
    for j in range(2):
    print " Inner loop executing. j = %d"%j
    if i == 3:
    raise stop
    print "Loop exited. i = %d"%i # message does not print
    except stop:
    print "Jumped to stop. i = %d"%i


    Regards,
    Kay
    Kay Schluehr, Jul 18, 2005
    #4
  5. On Mon, Jul 18, 2005 at 08:40:16AM -0700, Kay Schluehr wrote:
    > Hayri ERDENER schrieb:
    > > hi,
    > > what is the equivalent of C languages' goto statement in python?
    > > best regards

    >
    > No, but some of goto's use cases can be covered by unconditional jumps
    > provided by exceptions. [...]


    I like the "named loops" concept of other HLL like Ada 95 or Java better
    than either goto or exceptions. It allows you to use "break" and
    "continue" for other than the innermost loops, too:

    break; => break out of inner loop
    break loop_name; => break out of named loop "loop_name"

    OTOH it's not used *that* often, so I won't argue for including it in
    Python ;)

    -- Gerhard
    --
    Gerhard Häring - - Python, web & database development

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFC29EEdIO4ozGCH14RAvXeAKC5p8zGuOeI6OcsRNaS5nq+k9/ZyQCgtA2n
    ngCyiXRD5Z8PIauStFNBwKg=
    =+91Q
    -----END PGP SIGNATURE-----
    Gerhard Haering, Jul 18, 2005
    #5
  6. On Mon, 18 Jul 2005 14:48:08 +0300, Hayri ERDENER
    <> declaimed the following in comp.lang.python:

    > hi,
    > what is the equivalent of C languages' goto statement in python?
    > best regards


    A Google search on "Python comefrom" has a few hits...

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Jul 18, 2005
    #6
  7. Hayri ERDENER wrote:
    > what is the equivalent of C languages' goto statement in python?


    Download the goto module:
    http://www.entrian.com/goto/
    And you can use goto to your heart's content. And to the horror of all
    your friends/coworkers. ;)

    STeVe
    Steven Bethard, Jul 18, 2005
    #7
  8. Hayri ERDENER

    rbt Guest

    On Mon, 2005-07-18 at 12:27 -0600, Steven Bethard wrote:
    > Hayri ERDENER wrote:
    > > what is the equivalent of C languages' goto statement in python?

    >
    > Download the goto module:
    > http://www.entrian.com/goto/
    > And you can use goto to your heart's content. And to the horror of all
    > your friends/coworkers. ;)
    >
    > STeVe


    Shouldn't that be "to the horror of all your goto-snob friends."

    IMO, most of the people who deride goto do so because they heard or read
    where someone else did.

    Many of the world's most profitable software companies (MS for example)
    have thousands of goto statements in their code... oh the horror of it
    all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
    as these obviously ignorant companies?
    rbt, Jul 18, 2005
    #8
  9. Hayri ERDENER

    Dan Bishop Guest

    rbt wrote:
    > On Mon, 2005-07-18 at 12:27 -0600, Steven Bethard wrote:
    > > Hayri ERDENER wrote:
    > > > what is the equivalent of C languages' goto statement in python?

    > >
    > > Download the goto module:
    > > http://www.entrian.com/goto/
    > > And you can use goto to your heart's content. And to the horror of all
    > > your friends/coworkers. ;)

    >
    > Shouldn't that be "to the horror of all your goto-snob friends."
    >
    > IMO, most of the people who deride goto do so because they heard or read
    > where someone else did.


    Or because they've seen some BASIC programs written ca. 1980.
    Dan Bishop, Jul 18, 2005
    #9
  10. rbt wrote:
    > Steven Bethard wrote:
    >
    >>Download the goto module:
    >> http://www.entrian.com/goto/
    >>And you can use goto to your heart's content. And to the horror of all
    >>your friends/coworkers. ;)

    >
    > Shouldn't that be "to the horror of all your goto-snob friends."
    >
    > IMO, most of the people who deride goto do so because they heard or read
    > where someone else did.
    >
    > Many of the world's most profitable software companies (MS for example)
    > have thousands of goto statements in their code... oh the horror of it
    > all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
    > as these obviously ignorant companies?


    Perhaps I should reiterate: ;) Oh, and ;)

    The CPython source code is filled with gotos -- with a rough grep count,
    I found over 2000. And, while I wouldn't care to inspect them all, I'd
    guess that they're mostly quite appropriate. A lot of them look like
    "goto fail", "goto finally", etc. and are used to handle error
    conditions. Heck, the patch I wrote to add key= arguments to min() and
    max() in Python 2.5 uses goto for exactly this purpose.

    That said, I don't think my joke was really that inaccurate -- I have
    yet to see a good use case for gotos in Python. Why use gotos when you
    have an efficient exception handling mechanism?

    STeVe
    Steven Bethard, Jul 18, 2005
    #10
  11. On Mon, 18 Jul 2005 16:37:57 -0400, rbt wrote:

    > Shouldn't that be "to the horror of all your goto-snob friends."
    >
    > IMO, most of the people who deride goto do so because they heard or read
    > where someone else did.


    Or because they actually programmed in languages that used goto for flow
    control. Some of us have even programmed in versions of BASIC that didn't
    have FOR or WHILE loops, you had to roll your own out of gotos.

    How about you? How many times have you had to maintain and debug spaghetti
    code sprinkled with gotos all over the place?

    > Many of the world's most profitable software companies (MS for example)
    > have thousands of goto statements in their code... oh the horror of it
    > all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
    > as these obviously ignorant companies?


    You know, I agree with you here. Why, just the other day I was discussing
    computer systems with my Aunt Tilly, who wanted to buy a computer so she
    could send emails to her friends and relatives. I suggested she buy a Mac,
    and she said "Well, I thought about buying a Mac, but I'm afraid I can't
    possibly use any software that wasn't written using goto. So I'm going to
    buy a Windows machine. Do you recommend any anti-virus and anti-spyware
    software that's easy to use?"


    --
    Steven.
    Steven D'Aprano, Jul 18, 2005
    #11
  12. Hayri ERDENER

    Mike Meyer Guest

    rbt <> writes:
    > Many of the world's most profitable software companies (MS for example)
    > have thousands of goto statements in their code... oh the horror of it
    > all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
    > as these obviously ignorant companies?


    Because profitability has *nothing* to do with code quality, and
    everything to do with marketing. MS, in particular, has done an
    excellent job of divorcing code quality from their bottom line by
    shuffling the bulk of the support work off to other companies:
    hardware vendors who bundle MS software, system integrators, and
    customers friends and family being very high on the list.

    That they felt the need to do this speaks volumes about their code
    quality.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Jul 19, 2005
    #12
  13. Steven Bethard wrote:

    > Hayri ERDENER wrote:
    >> what is the equivalent of C languages' goto statement in python?

    >
    > Download the goto module:
    > http://www.entrian.com/goto/
    > And you can use goto to your heart's content. And to the horror of all
    > your friends/coworkers. ;)
    >
    > STeVe


    That is actually a _really_ cool piece of code, in terms of showing off the
    kind of things which are possible in python if you're willing to be a little
    sneaky.

    Thanks for the pointer !

    best

    f
    Fernando Perez, Jul 19, 2005
    #13
  14. Hayri ERDENER

    D H Guest

    Mike Meyer wrote:
    > rbt <> writes:
    >
    >>Many of the world's most profitable software companies (MS for example)
    >>have thousands of goto statements in their code... oh the horror of it
    >>all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
    >>as these obviously ignorant companies?

    >
    >
    > Because profitability has *nothing* to do with code quality, and
    > everything to do with marketing. MS, in particular, has done an
    > excellent job of divorcing code quality from their bottom line by
    > shuffling the bulk of the support work off to other companies:
    > hardware vendors who bundle MS software, system integrators, and
    > customers friends and family being very high on the list.
    >
    > That they felt the need to do this speaks volumes about their code
    > quality.


    whoa whoa whoa! Discussing goto statements and Microsoft together is
    like mixing dynamite and gasoline. We don't want this to explode into
    some interminable argument; that's not the kind of thing people like to
    see on comp.lang.python.

    Oh by the way, boo has goto statements:
    http://svn.boo.codehaus.org/trunk/tests/testcases/integration/goto-1.boo?view=auto

    KABOOM
    D H, Jul 19, 2005
    #14
  15. rbt wrote:
    > IMO, most of the people who deride goto do so because they heard or read
    > where someone else did.


    1 GOTO 17
    2 mean, GOTO 5
    3 could GOTO 6
    4 with GOTO 7
    5 what GOTO 3
    6 possibly GOTO 24
    7 you! GOTO 21
    8 that GOTO 18
    9 really, GOTO 23
    10 understandable?
    11 neat. GOTO 16
    12 and GOTO 25
    13 are GOTO 9
    14 I GOTO 26
    15 wrong GOTO 20
    16 I GOTO 2
    17 Yes, GOTO 14
    18 simple GOTO 12
    19 agree GOTO 4
    20 with GOTO 22
    21 Gotos GOTO 13
    22 something GOTO 8
    23 really GOTO 11
    24 be GOTO 15
    25 easily GOTO 10
    26 totally GOTO 19
    Leif K-Brooks, Jul 19, 2005
    #15
  16. Hayri ERDENER

    rbt Guest

    10 PRINT "YOU'RE NOT RIGHT IN THE HEAD."
    20 GOTO 10


    On Tue, 2005-07-19 at 02:33 +0000, Leif K-Brooks wrote:
    > rbt wrote:
    > > IMO, most of the people who deride goto do so because they heard or read
    > > where someone else did.

    >
    > 1 GOTO 17
    > 2 mean, GOTO 5
    > 3 could GOTO 6
    > 4 with GOTO 7
    > 5 what GOTO 3
    > 6 possibly GOTO 24
    > 7 you! GOTO 21
    > 8 that GOTO 18
    > 9 really, GOTO 23
    > 10 understandable?
    > 11 neat. GOTO 16
    > 12 and GOTO 25
    > 13 are GOTO 9
    > 14 I GOTO 26
    > 15 wrong GOTO 20
    > 16 I GOTO 2
    > 17 Yes, GOTO 14
    > 18 simple GOTO 12
    > 19 agree GOTO 4
    > 20 with GOTO 22
    > 21 Gotos GOTO 13
    > 22 something GOTO 8
    > 23 really GOTO 11
    > 24 be GOTO 15
    > 25 easily GOTO 10
    > 26 totally GOTO 19
    rbt, Jul 19, 2005
    #16
  17. Fernando Perez wrote:
    > Steven Bethard wrote:
    >
    >>Download the goto module:
    >> http://www.entrian.com/goto/
    >>And you can use goto to your heart's content. And to the horror of all
    >>your friends/coworkers. ;)

    >
    > That is actually a _really_ cool piece of code, in terms of showing off the
    > kind of things which are possible in python if you're willing to be a little
    > sneaky.


    Yeah, it's pretty slick. I think most people who see the link don't
    realize that it's actually *working code* for gotos in Python.

    STeVe
    Steven Bethard, Jul 19, 2005
    #17
  18. rbt enlightened us with:
    > Many of the world's most profitable software companies (MS for
    > example) have thousands of goto statements in their code... oh the
    > horror of it all. Why aren't these enlightened-by-the-gods
    > know-it-alls as profitable as these obviously ignorant companies?


    They write software with huge security holes. Direct3D still isn't as
    stable as OpenGL. It takes ages for them to create security patches.

    The things I mention are *not* caused by their nice and clean way of
    coding.

    As a matter of fact, they use goto to jump from one function to
    another! And to make sure a 'return' doesn't return to the last call,
    but to some other, they combine this awful use of goto with manual
    stack manipulation. And they do all of this in C (or some derivative)
    so if one function changes it's parameters, all the manual stack
    modifications and gotos need to be checked for correctness.

    I'd rather use an exception, or better even - write small functions so
    I can call 'return' instead of doing 'goto EXIT'.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Jul 19, 2005
    #18
  19. Hayri ERDENER

    Robert Kern Guest

    rbt wrote:

    > IMO, most of the people who deride goto do so because they heard or read
    > where someone else did.


    Or perhaps, like me, they have had to maintain FORTRAN code written by a
    scientist who apparently hadn't heard of subroutines. "Spaghetti"
    doesn't quite describe it. I've settled on "Lovecraftian": reading the
    code, you can't help but get the impression of writhing tentacles and
    impossible angles.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Jul 19, 2005
    #19
  20. Leif K-Brooks wrote:
    > rbt wrote:
    >
    >>IMO, most of the people who deride goto do so because they heard or read
    >>where someone else did.

    >
    >
    > 1 GOTO 17
    > 2 mean, GOTO 5
    > 3 could GOTO 6
    > 4 with GOTO 7
    > 5 what GOTO 3
    > 6 possibly GOTO 24
    > 7 you! GOTO 21
    > 8 that GOTO 18
    > 9 really, GOTO 23
    > 10 understandable?
    > 11 neat. GOTO 16
    > 12 and GOTO 25
    > 13 are GOTO 9
    > 14 I GOTO 26
    > 15 wrong GOTO 20
    > 16 I GOTO 2
    > 17 Yes, GOTO 14
    > 18 simple GOTO 12
    > 19 agree GOTO 4
    > 20 with GOTO 22
    > 21 Gotos GOTO 13
    > 22 something GOTO 8
    > 23 really GOTO 11
    > 24 be GOTO 15
    > 25 easily GOTO 10
    > 26 totally GOTO 19


    I dislike gotos because it is too easy to inadvertently create infinite
    loops. <10 WINK; 20 GOTO 10>
    Rocco Moretti, Jul 19, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. youngsun park
    Replies:
    2
    Views:
    614
    David Pursley
    Nov 18, 2003
  2. Skybuck Flying

    VHDL Goto statement ?

    Skybuck Flying, Aug 8, 2005, in forum: VHDL
    Replies:
    9
    Views:
    6,094
    ajahn
    Aug 26, 2005
  3. Skybuck Flying

    Re: VHDL Goto statement ?

    Skybuck Flying, Aug 8, 2005, in forum: VHDL
    Replies:
    0
    Views:
    727
    Skybuck Flying
    Aug 8, 2005
  4. Flip
    Replies:
    6
    Views:
    6,658
    Jeff Dillon
    Nov 18, 2004
  5. Flip
    Replies:
    1
    Views:
    631
    Brock Allen
    Apr 14, 2005
Loading...

Share This Page