From: Phil Carmody on
James Kanze <james.kanze(a)gmail.com> writes:
> On Apr 24, 1:20 pm, "Leigh Johnston" <le...(a)i42.co.uk> wrote:
>> "spinoza1111" <spinoza1...(a)yahoo.com> wrote in message
>
>> news:051fa27d-5cf8-4f7e-94c8-e9e5b26566b7(a)g34g2000pro.googlegroups.com...
>
>> > On Apr 24, 6:06 pm, Ali Karaali <ali...(a)gmail.com> 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?

For the same reason you want to break out of a single loop.
Do you have issues with people breaking out of single loops?

> (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.)

What do you mean by 'otherwise' - even that's just a goto in disguise?

Phil
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1
From: Phil Carmody on
Andrew Poelstra <apoelstra(a)localhost.localdomain> writes:
> 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;

You forgot to do the unsetup3(), unsetup2(), and unsetup1() as
appropriate for the setup*() that succeeded above. Congratulations,
you've leaked memory, left a file open, and locked a semaphore.

> Personally, I would do this as:
>
> if(setup1() &&
> setup2() &&
> setup3()) {
> /* now do stuff */
> return 0;
> }
> else {
> /* :( */
> return 1;
> }

Where would the unsetup3() call go in the above?

> 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.

I never particularly liked goto, but sometimes they are the simplest
and most readable thing to do. It's the standard in the project I'm
currently working on, for example. Far better than disguised gotos
such as do { /* ... */ break; /* ... */ } while(0);

Phil
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1
From: wolfgang kern on

"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

Except that GOTO is a HLL keyword and I'm Asmist and
more machine-code oriented than concerned about needs
within abstract structured HLL-compilers, I see nothing
wrong with GOTO as long it does what a programmer want.

I once checked PowerBasic's GOTO and found it always
compiled a long (16:16) jump, even rare required.
But its syntax checker cried aloud if you tried to
jump out of a [FOR/IF/...]-struct with GOTO.

I don't know C/C+-, but what I see by disassembling their
resulting output seem to end up in heavy bloated code,
perhaps from using BREAK and GOTO within nested blocks,
which the compiler may grant with copying all required
code-trails (stack recover/cleanup) to every added path.
So in this aspect GOTO may be really not the best choice.

see me happy for I don't need to care this in my world :)
__
wolfgang (ALA)










From: Patricia Shanahan on
Alexei A. Frounze wrote:
....
> Surprisingly, my code is pretty clean with gotos, which, btw, I use
> just for almost exclusively 2 things:
> - breaking out of nested loops (not always, if I can do that w/o
> writing more than two lines of code, I don't use goto)
> - jumping to the common cleanup code at the end of the function when I
> detect some error
....

When I was first considering using Java for a project, I was a little
worried about the lack of goto.

I knew I had written a couple of gotos in C and maintained code
containing goto that would have had to be much more convoluted without
it. When I analyzed those uses of goto, I realized they were all in
those two categories, mainly the common cleanup code case.

That meant I would not need goto in a language with try-finally and
named loop break.

Patricia
From: Daniel T. on
Ben Bacarisse <ben.usenet(a)bsb.me.uk> wrote:

> I like to imagine that there is something akin to good taste in
> programming. Even people with impeccable taste will swear on occasion
> if the situation merits it; so programmers who code tastefully will
> sometimes use continue or break.

I like your analogy. Only use continue, break or goto when appropriate.
The problem is that all of us agree with that sentiment (even Dijkstra
didn't say it should be outlawed completely,) the debate is about how
much use is appropriate.