Re: on goto

Discussion in 'C Programming' started by Ali Karaali, Apr 24, 2010.

  1. Ali Karaali

    Ali Karaali Guest

    I use goto to break nested for loops and I can't see a
    reason to ban goto.

    Ali

    On 24 Nisan, 13:13, "io_x" <> wrote:
    > [Xpost to: alt.comp.programming, alt.lang.asm,
    > comp.lang.c, comp.lang.c++, comp.programming]
    >
    > the people that speak not good about "goto"
    > are uttled wrong; their code bug full etc
    >
    > more distant is your code from goto
    > more your code is bug full and incompresible
    >
    > the "goto" is the key word for programming;
    > all the remain, other than goto or jmp or jc or jz,
    > and the easy cpu layout
    > is the wrong way for programming
    >
    > i advise all you
    > Good Morning
    Ali Karaali, Apr 24, 2010
    #1
    1. Advertising

  2. Ali Karaali

    Tim Streater Guest

    In article
    <>,
    Ali Karaali <> wrote:

    > I use goto to break nested for loops and I can't see a
    > reason to ban goto.
    >
    > Ali
    >
    > On 24 Nisan, 13:13, "io_x" <> wrote:
    > > [Xpost to: alt.comp.programming, alt.lang.asm,
    > > comp.lang.c, comp.lang.c++, comp.programming]
    > >
    > > the people that speak not good about "goto"
    > > are uttled wrong; their code bug full etc
    > >
    > > more distant is your code from goto
    > > more your code is bug full and incompresible
    > >
    > > the "goto" is the key word for programming;
    > > all the remain, other than goto or jmp or jc or jz,
    > > and the easy cpu layout
    > > is the wrong way for programming
    > >
    > > i advise all you
    > > Good Morning


    I haven't used a goto since 1978 (when I stopped writing FORTRAN).

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
    Tim Streater, Apr 24, 2010
    #2
    1. Advertising

  3. Ali Karaali

    spinoza1111 Guest

    On Apr 24, 6:06 pm, Ali Karaali <> wrote:
    > I use goto to break nested for loops and I can't see a
    > reason to ban goto.


    Why not use break instead? Does the same thing, spares you from having
    to define a label.

    There are two senses in which a program can be structured.

    The first is syntactical, and to be syntactically structured, the
    source code of the program must be restricted to structured syntax, of
    which neither goto nor break is a strict example. You'd have to
    simulate the break logic:

    broken = false;
    for(!broken && condition)
    {
    .
    .
    .
    if (exceptional condition) broken = true;
    else
    {
    .
    .
    .
    }
    }

    The second is semantic. To be structured in this way, the program must
    be decomposable without change into blocks, each at most one entry and
    at most one exit, and each of which is one of the Bohm-Jacopini
    constructs process, if..then..else, or do while.

    If the program is syntactically structured, it must be semantically
    structured. But the reverse is not true; an assembler program using go
    to can be semantically structured but it is not syntactically
    structured, like this inefficient and contrived example

    LOAD 0
    STOR i
    LBL1:
    LOAD i
    COMP limit
    IFGE LBL19
    ..
    ..
    ..
    IFEQ LBL12
    LOAD j
    STOR v
    GOTO LBL129
    LBL12:
    LOAD k
    STOR v2
    LBL129:
    ..
    ..
    ..
    LOAD i
    ADD 1
    STOR i
    GOTO LBL1
    LBL19:

    Then, certain extensions are permitted including most famously case
    without fallthrough, one trip do "until", and break, implemented
    semantically with a go to to a label one past the end of the loop or
    syntactically with break.

    A breaking loop is a new structure relative to the three Bohm-Jacopini
    structures, switch case and one trip do. And it "weakens" the
    postconditions that must obtain on exit. Most famously, the index of a
    breaking loop may not be one past the end of what was being processed.
    >
    > Ali
    >
    > On 24 Nisan, 13:13, "io_x" <> wrote:
    >
    >
    >
    > > [Xpost to: alt.comp.programming, alt.lang.asm,
    > > comp.lang.c, comp.lang.c++, comp.programming]

    >
    > > the people that speak not good about "goto"
    > > are uttled wrong; their code bug full etc

    >
    > > more distant is your code from goto
    > > more your code is bug full and incompresible

    >
    > > the "goto" is the key word for programming;
    > > all the remain, other than goto or jmp or jc or jz,
    > > and the easy cpu layout
    > > is the wrong way for programming

    >
    > > i advise all you
    > > Good Morning
    spinoza1111, Apr 24, 2010
    #3
  4. Ali Karaali

    bartc Guest

    "Daniel T." <> wrote in message
    news:...
    > Ali Karaali <> wrote:
    >
    >> I use goto to break nested for loops and I can't see a
    >> reason to ban goto.

    >
    > I'll give you a reason to ban goto. This is a line of BASIC code:
    >
    > 90 x = 5
    >
    > Now, I ask you, where is the line that was previously executed? Is it
    > directly above line 90, is it directly below it, or could it be anywhere
    > in the program? Because of the goto statement, simply because an
    > unstructured "goto" is possible in the language, you have to analyze the
    > entire program to know the answer. If we ban the use of "goto", we can
    > confidently know exactly what line of code comes before line 90.


    Goto's at least are usually confined to the same function; if the function
    is smallish, you don't have to look far to match a goto with a label.

    Most languages will allow "x=5" inside a function; where is that function
    called from? It could be from anywhere in hundreds of modules.

    --
    Bartc
    bartc, Apr 24, 2010
    #4
  5. In article <>,
    Leigh Johnston <> wrote:
    ....
    >This channel is about C++ not BASIC, you do not have to analyze the entire
    >program in C++ as goto can only jump within the same function so comparisons
    >with BASIC is silly.


    (Channelling Kiki)

    >This channel


    channel???

    >is about C++ not BASIC, you do not have to analyze the entire


    C++ is not C (so says Kiki every chance he gets)

    --
    > No, I haven't, that's why I'm asking questions. If you won't help me,
    > why don't you just go find your lost manhood elsewhere.


    CLC in a nutshell.
    Kenny McCormack, Apr 24, 2010
    #5
  6. Ali Karaali

    spinoza1111 Guest

    On Apr 24, 11:50 pm, (Kenny McCormack)
    wrote:
    > In article <>,Leigh Johnston <> wrote:
    >
    > ...
    >
    > >This channel is about C++ not BASIC, you do not have to analyze the entire
    > >program in C++ as goto can only jump within the same function so comparisons
    > >with BASIC is silly.

    >
    > (Channelling Kiki)


    Shouldn't that be Mr. Thompson?
    >
    > >This channel

    >
    > channel???


    More like Cartoon Network's Scoobie Doo Weekend.
    >
    > >is about C++ not BASIC, you do not have to analyze the entire

    >
    > C++ is not C (so says Kiki every chance he gets)


    Surely you're shittin' me
    Surely you jest
    C plus plus not C?
    Omigod is that on the test?

    Surely that ole Kiki
    Is a real giant brain
    If he knows shit like that 'bout C
    And how to code, int main()

    Surely I'm not worthy
    Surely I'm scum
    Gotta listen to Kiki
    Ooooooohhhh he makes me...get all excited or something...

    Kiki made a wiki
    On wikipedi-AH
    About a god named tiki
    And how to code in C-ah

    But then an egregious Troll
    Thought it would be Droll
    To not call him Thompson
    So Kiki got his machine gun
    And he kilt the guy, Spinoza Ed
    He kilt him so he was dead dead dead

    This story has no moral
    This story has no end
    This story just goes to show
    That Kiki's gone round the bend

    (Dissolve to burning tires and dancing trolls)
    >
    > --
    >
    > > No, I haven't, that's why I'm asking questions. If you won't help me,
    > > why don't you just go find your lost manhood elsewhere.

    >
    > CLC in a nutshell.
    spinoza1111, Apr 24, 2010
    #6
  7. Ali Karaali

    spinoza1111 Guest

    On Apr 24, 11:48 pm, "Leigh Johnston" <> wrote:
    > "Daniel T." <> wrote in message
    >
    > news:...
    >
    > > Ali Karaali <> wrote:

    >
    > >> I use goto to break nested for loops and I can't see a
    > >> reason to ban goto.

    >
    > > I'll give you a reason to ban goto. This is a line of BASIC code:

    >
    > > 90 x = 5

    >
    > > Now, I ask you, where is the line that was previously executed? Is it
    > > directly above line 90, is it directly below it, or could it be anywhere
    > > in the program? Because of the goto statement, simply because an
    > > unstructured "goto" is possible in the language, you have to analyze the
    > > entire program to know the answer. If we ban the use of "goto", we can
    > > confidently know exactly what line of code comes before line 90.

    >
    > This channel is about C++ not BASIC, you do not have to analyze the entire
    > program in C++ as goto can only jump within the same function so comparisons
    > with BASIC is silly.


    That's been UNTRUE about the only Basic left standing (Visual Basic)
    for at least twenty years, pard. Basic programs can be written as a
    single main() with go tos, but so can C programs. Either style of
    coding is possible if one's an idiot savant, twisted sick freak,
    moron, or down a quart or more on a couple of computer science
    classes.

    However, just like C, modern Basic includes Function and Sub
    constructs and structured programming. Indeed, buy my book I need the
    money; it is "Build Your Own .Net Language and Compiler" (Apress 2004)
    and it includes a fully structured and OO compiler for much of Quick
    Basic, organized into functions and written in Visual Basic 2003. I
    don't recall using Go To at all.

    (Anybody who writes a compiler in Basic, even modern, structured
    Basic, should either have his head examined or win some sort of Geek O
    Rama prize, but when I asked my technical editor if I could write it
    in C Sharp he said no.)
    >
    > /Leigh
    spinoza1111, Apr 24, 2010
    #7
  8. Ali Karaali

    spinoza1111 Guest

    On Apr 24, 11:46 pm, "bartc" <> wrote:
    > "Daniel T." <> wrote in message
    >
    > news:...
    >
    > > Ali Karaali <> wrote:

    >
    > >> I use goto to break nested for loops and I can't see a
    > >> reason to ban goto.

    >
    > > I'll give you a reason to ban goto. This is a line of BASIC code:

    >
    > > 90 x = 5

    >
    > > Now, I ask you, where is the line that was previously executed? Is it
    > > directly above line 90, is it directly below it, or could it be anywhere
    > > in the program? Because of the goto statement, simply because an
    > > unstructured "goto" is possible in the language, you have to analyze the
    > > entire program to know the answer. If we ban the use of "goto", we can
    > > confidently know exactly what line of code comes before line 90.

    >
    > Goto's at least are usually confined to the same function; if the function
    > is smallish, you don't have to look far to match a goto with a label.


    It's not a question of how far you have to look. It is a question of
    minimal preconditions. If you have a go to even in a small program,
    the code that follows it has a larger set of preconditions.
    >
    > Most languages will allow "x=5" inside a function; where is that function
    > called from? It could be from anywhere in hundreds of modules.
    >
    > --
    > Bartc
    spinoza1111, Apr 24, 2010
    #8
  9. Ali Karaali

    spinoza1111 Guest

    On Apr 25, 12:27 am, "Daniel T." <> wrote:
    > "bartc" <> wrote:
    > > "Daniel T." <> wrote:
    > > > Ali Karaali <> wrote:

    >
    > > > > I use goto to break nested for loops and I can't see a reason to
    > > > > ban goto.

    >
    > > > I'll give you a reason to ban goto. This is a line of BASIC code:

    >
    > > > 90 x = 5

    >
    > > > Now, I ask you, where is the line that was previously executed? Is
    > > > it directly above line 90, is it directly below it, or could it be
    > > > anywhere in the program? Because of the goto statement, simply
    > > > because an unstructured "goto" is possible in the language, you have
    > > > to analyze the entire program to know the answer. If we ban the use
    > > > of "goto", we can confidently know exactly what line of code comes
    > > > before line 90.

    >
    > > Goto's at least are usually confined to the same function; if the
    > > function is smallish, you don't have to look far to match a goto with
    > > a label.

    >
    > > Most languages will allow "x=5" inside a function; where is that
    > > function called from? It could be from anywhere in hundreds of modules.

    >
    > As Dijkstra pointed out in "Go To Statement Considered Harmful"
    >
    >    As soon as we include in our language procedures we must admit that
    >    a single textual index is no longer sufficient. In the case that a
    >    textual index points to the interior of a procedure body the dynamic
    >    progress is only characterized when we also give to which call of
    >    the procedure we refer. With the inclusion of procedures we can
    >    characterize the progress of the process via a sequence of textual
    >    indices, the length of this sequence being equal to the dynamic
    >    depth of procedure calling.
    >
    >    ...
    >
    >    The main point is that the values of these indices are outside
    >    programmer's control; they are generated (either by the write-up of
    >    his program or by the dynamic evolution of the process) whether he
    >    wishes or not. They provide independent coordinates in which to
    >    describe the progress of the process.
    >
    >    Why do we need such independent coordinates? The reason is - and
    >    this seems to be inherent to sequential processes - that we can
    >    interpret the value of a variable only with respect to the progress
    >    of the process. If we wish to count the number, n say, of people in
    >    an initially empty room, we can achieve this by increasing n by one
    >    whenever we see someone entering the room. In the in-between moment
    >    that we have observed someone entering the room but have not yet
    >    performed the subsequent increase of n, its value equals the number
    >    of people in the room minus one!
    >
    >    The unbridled use of the go to statement has an immediate
    >    consequence that it becomes terribly hard to find a meaningful set
    >    of coordinates in which to describe the process progress. Usually,
    >    people take into account as well the values of some well chosen
    >    variables, but this is out of the question because it is relative to
    >    the progress that the meaning of these values is to be understood!
    >    With the go to statement one can, of course, still describe the
    >    progress uniquely by a counter counting the number of actions
    >    performed since program start (viz. a kind of normalized clock). The
    >    difficulty is that such a coordinate, although unique, is utterly
    >    unhelpful. In such a coordinate system it becomes an extremely
    >    complicated affair to define all those points of progress where,
    >    say, n equals the number of persons in the room minus one!
    >
    > Your comparison between functions and goto has already shown to be
    > unfounded.


    Brilliant, couldn't say it any better.

    Programming, and coding's laws lay hidden in night:
    God said, let Dijkstra be! And all was Light.

    DIJSTRA! thou shouldst be living at this hour:
    CLC hath need of thee: she is a fen
    Of stagnant waters: fighting about void main
    Programmers, once proud of their might and power
    Have forfeited their ancient English dower 5
    Of simple competence. We are selfish men;
    O raise us up, return to us again,
    And give us manners, virtue, freedom, power!
    Thy soul was like a Star, and dwelt apart;
    Thou hadst a voice whose sound was like the sea: 10
    Pure as the naked heavens, majestic, free,
    So didst thou travel on life's common way,
    In cheerful godliness; and yet thy heart
    The lowliest duties on herself did lay:
    Even business programming you deigned to address
    And at assembling railway trains you were a success:
    You showed us how to make sure the Loo
    Was equidistant from each chap, in the choo choo. [1]
    Come back into the building, Dijkstra, like a God
    Returning to earth to redeem both wise man and Clod.

    [1] EWD365: A Parable, at http://userweb.cs.utexas.edu/users/EWD/transcriptions/EWD03xx/EWD365.html
    spinoza1111, Apr 24, 2010
    #9
  10. Ali Karaali

    Moi Guest

    On Sat, 24 Apr 2010 13:06:44 -0400, Daniel T. wrote:

    > spinoza1111 <> wrote:
    >> On Apr 24, 11:46 pm, "bartc" <> wrote:
    >> > "Daniel T." <> wrote:
    >> > > Ali Karaali <> wrote:
    >> >


    >>
    >> It's not a question of how far you have to look. It is a question of
    >> minimal preconditions. If you have a go to even in a small program, the
    >> code that follows it has a larger set of preconditions.

    >
    > Could you go into more detail? Maybe with some examples?


    IMHO the *readability* of "goto cleanup;" is not basically different
    from a function call "do_cleanup();".
    A function does not check its preconditions either.
    In both cases the programmer can choose a name for the function or label.
    And that name may or may not reflect its purpose.

    AvK
    Moi, Apr 24, 2010
    #10
  11. Ali Karaali

    Seebs Guest

    On 2010-04-24, Daniel T. <> wrote:
    > I'll give you a reason to ban goto. This is a line of BASIC code:
    >
    > 90 x = 5
    >
    > Now, I ask you, where is the line that was previously executed? Is it
    > directly above line 90, is it directly below it, or could it be anywhere
    > in the program? Because of the goto statement, simply because an
    > unstructured "goto" is possible in the language, you have to analyze the
    > entire program to know the answer. If we ban the use of "goto", we can
    > confidently know exactly what line of code comes before line 90.


    That's a great reason, but it's got nothing to do with C. In C, only
    other code within the same function can have gone to this line, and that
    only if there is a label immediately before this line.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Apr 24, 2010
    #11
  12. "Daniel T." <> writes:

    > Ali Karaali <> wrote:
    >
    >> I use goto to break nested for loops and I can't see a
    >> reason to ban goto.

    >
    > I'll give you a reason to ban goto. This is a line of BASIC code:
    >
    > 90 x = 5
    >
    > Now, I ask you, where is the line that was previously executed? Is it
    > directly above line 90, is it directly below it, or could it be anywhere
    > in the program? Because of the goto statement, simply because an
    > unstructured "goto" is possible in the language, you have to analyze the
    > entire program to know the answer. If we ban the use of "goto", we can
    > confidently know exactly what line of code comes before line 90.


    You have to EXECUTE the entire program before knowing where you came
    from, thanks to computed GOTOs :)

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Apr 24, 2010
    #12
  13. Ali Karaali

    Tim Streater Guest

    In article <-september.org>,
    Richard <> wrote:

    > Seebs <> writes:
    >
    > > On 2010-04-24, Daniel T. <> wrote:
    > >> I'll give you a reason to ban goto. This is a line of BASIC code:
    > >>
    > >> 90 x = 5
    > >>
    > >> Now, I ask you, where is the line that was previously executed? Is it
    > >> directly above line 90, is it directly below it, or could it be anywhere
    > >> in the program? Because of the goto statement, simply because an
    > >> unstructured "goto" is possible in the language, you have to analyze the
    > >> entire program to know the answer. If we ban the use of "goto", we can
    > >> confidently know exactly what line of code comes before line 90.

    > >
    > > That's a great reason, but it's got nothing to do with C. In C, only

    >
    > It's a ridiculous reason. The search for the goto LABEL is trivial and a
    > goto is often a LOT more obvious that some convoluted condition based
    > branch using lots of nested brackets.
    >
    > > other code within the same function can have gone to this line, and that
    > > only if there is a label immediately before this line.
    > >
    > > -s

    >
    > There is nothing wrong with Gotos when used in a controlled and
    > intelligent manner. Like the bullshit about single return points the
    > idea that gotos should not be used is generally just from 3rd rate
    > students who think it proves they program "structured" ....


    Like Spinny, you mean, with his "structured switch" twaddle.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
    Tim Streater, Apr 24, 2010
    #13
  14. Ali Karaali

    Phil Carmody Guest

    Tim Streater <> writes:
    > In article
    > <>,
    > Ali Karaali <> wrote:
    >
    >> I use goto to break nested for loops and I can't see a
    >> reason to ban goto.
    >>
    >> Ali
    >>
    >> On 24 Nisan, 13:13, "io_x" <> wrote:
    >> > [Xpost to: alt.comp.programming, alt.lang.asm,
    >> > comp.lang.c, comp.lang.c++, comp.programming]
    >> >
    >> > the people that speak not good about "goto"
    >> > are uttled wrong; their code bug full etc
    >> >
    >> > more distant is your code from goto
    >> > more your code is bug full and incompresible
    >> >
    >> > the "goto" is the key word for programming;
    >> > all the remain, other than goto or jmp or jc or jz,
    >> > and the easy cpu layout
    >> > is the wrong way for programming
    >> >
    >> > i advise all you
    >> > Good Morning

    >
    > I haven't used a goto since 1978 (when I stopped writing FORTRAN).


    I'm with Knuth. I've reviewed patches and told the submitter to
    just convert all his jumble of 'structure' into nice clean goto
    statements many times this year. I don't use it for my own code,
    but $DAYJOB has a clear coding style that recommends goto in
    many situations. If goto has made your function unreadable, it
    was probably too big and unreadable anyway.

    Phil
    --
    I find the easiest thing to do is to k/f myself and just troll away
    -- David Melville on r.a.s.f1
    Phil Carmody, Apr 24, 2010
    #14
  15. Ali Karaali

    James Kanze Guest

    On Apr 24, 10:26 pm, Phil Carmody <>
    wrote:
    > Tim Streater <> writes:
    > > I haven't used a goto since 1978 (when I stopped writing
    > > FORTRAN).


    Me neither.

    > I'm with Knuth.


    In other words, if goto makes the code simpler, it means that
    you haven't found the correct expression of the algorithm.

    And of course, the problem isn't with goto, per se. It's with
    the way it is used. But because it can be abused so badly,
    modern languages (including C and C++) provide all of the
    necessary structures (and then some) so you don't have to use
    it.

    > I've reviewed patches and told the submitter to just convert
    > all his jumble of 'structure' into nice clean goto statements
    > many times this year. I don't use it for my own code, but
    > $DAYJOB has a clear coding style that recommends goto in many
    > situations. If goto has made your function unreadable, it was
    > probably too big and unreadable anyway.


    If goto seems necessary, it's because your function is too big
    and unreadable.

    --
    James Kanze
    James Kanze, Apr 24, 2010
    #15
  16. Ali Karaali

    James Kanze Guest

    On Apr 24, 1:20 pm, "Leigh Johnston" <> wrote:
    > "spinoza1111" <> wrote in message


    > news:...


    > > On Apr 24, 6:06 pm, Ali Karaali <> wrote:
    > >> I use goto to break nested for loops and I can't see a
    > >> reason to ban goto.


    > > Why not use break instead? Does the same thing, spares you
    > > from having to define a label.


    > Because break only breaks out of the innermost loop, using
    > goto to break out of nested loops is one of the few sensible
    > uses of goto.


    But when do you have to break out of a nested loop? (And of
    course, the only valid use of break in C++ is to end a case in a
    switch. Otherwise, it's just a goto in disguise.)

    --
    James Kanze
    James Kanze, Apr 24, 2010
    #16
  17. Ali Karaali

    James Kanze Guest

    On Apr 24, 5:12 pm, "Daniel T." <> wrote:
    > "Leigh Johnston" <> wrote:


    [...]
    > In C and C++, goto is sufficiently restricted that as long as
    > your functions are small, it is largely harmless.


    In C and C++, if your functions are small enough, goto is
    largely harmless. And also useless. All of the examples I've
    seen defending goto introduce excessively complex functions in
    order to justify it.

    --
    James Kanze
    James Kanze, Apr 24, 2010
    #17
  18. Ali Karaali

    bartc Guest

    spinoza1111 wrote:
    > On Apr 25, 12:27 am, "Daniel T." <> wrote:
    >> "bartc" <> wrote:
    >>> "Daniel T." <> wrote:
    >>>> Ali Karaali <> wrote:

    >>
    >>>>> I use goto to break nested for loops and I can't see a reason to
    >>>>> ban goto.


    >>>> 90 x = 5

    >>
    >>>> Now, I ask you, where is the line that was previously executed? Is


    >>> Goto's at least are usually confined to the same function


    >>> Most languages will allow "x=5" inside a function; where is that
    >>> function called from?


    >> As Dijkstra pointed out in "Go To Statement Considered Harmful"


    >> As soon as we include in our language procedures we must admit that
    >> a single textual index is no longer sufficient. In the case that a

    .....

    >> Your comparison between functions and goto has already shown to be
    >> unfounded.

    >
    > Brilliant, couldn't say it any better.


    That's a shame, because I didn't understand a word of the Dijkstra extract.

    --
    Bartc
    bartc, Apr 25, 2010
    #18
  19. On 2010-04-24, James Kanze <> wrote:
    > On Apr 24, 1:20 pm, "Leigh Johnston" <> wrote:
    >> "spinoza1111" <> wrote in message

    >
    >> news:...

    >
    >> > On Apr 24, 6:06 pm, Ali Karaali <> wrote:
    >> >> I use goto to break nested for loops and I can't see a
    >> >> reason to ban goto.

    >
    >> > Why not use break instead? Does the same thing, spares you
    >> > from having to define a label.

    >
    >> Because break only breaks out of the innermost loop, using
    >> goto to break out of nested loops is one of the few sensible
    >> uses of goto.

    >
    > But when do you have to break out of a nested loop? (And of
    > course, the only valid use of break in C++ is to end a case in a
    > switch. Otherwise, it's just a goto in disguise.)
    >


    [dropped alt.comp.programming and alt.lang.asm since my
    newsserver rejects an egregious number of cross-posts.]

    This is just as true in C, according to some people (myself
    included). The only exception I can think of is when you are
    looping only to set the loop index to something.

    ie,

    for(i = 0; i < 10; ++i)
    if(arr.state = READY)
    break;
    /* now use arr, or if i is 10, indicate failure. */

    The alternative here, using the loop structure, would be:

    for(i = 0; i < 10 && arr.state != READY; ++i)
    ;
    /* now use arr, or if i is 10, indicate failure. */

    To me, the latter option looks horrific, plus if you
    swap the operands of &&, you get a very hard-to-spot
    overrun.

    --
    Andrew Poelstra
    http://www.wpsoftware.net/andrew
    Andrew Poelstra, Apr 25, 2010
    #19
  20. On 2010-04-24, James Kanze <> wrote:
    > On Apr 24, 5:12 pm, "Daniel T." <> wrote:
    >> "Leigh Johnston" <> wrote:

    >
    > [...]
    >> In C and C++, goto is sufficiently restricted that as long as
    >> your functions are small, it is largely harmless.

    >
    > In C and C++, if your functions are small enough, goto is
    > largely harmless. And also useless. All of the examples I've
    > seen defending goto introduce excessively complex functions in
    > order to justify it.
    >


    [dropped alt.comp.programming and alt.lang.asm since my
    newsserver rejects an egregious number of cross-posts.]

    I often see code like

    if(setup1() == 0)
    goto fail;

    if(setup2() == 0)
    goto fail;

    if(setup3() == 0)
    goto fail;

    /* now do stuff */

    return 0;
    fail:
    /* :( */
    return 1;


    Personally, I would do this as:

    if(setup1() &&
    setup2() &&
    setup3()) {
    /* now do stuff */
    return 0;
    }
    else {
    /* :( */
    return 1;
    }

    But from a stylistic view, the first version doesn't seem all
    that bad. I see it a lot and it doesn't bother me, anyway.

    --
    Andrew Poelstra
    http://www.wpsoftware.net/andrew
    Andrew Poelstra, Apr 25, 2010
    #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:
    625
    David Pursley
    Nov 18, 2003
  2. Skybuck Flying

    VHDL Goto statement ?

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

    Re: VHDL Goto statement ?

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

Share This Page