Deutsch   English   Français   Italiano  
<87wmg4vaeb.fsf@nosuchdomain.example.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: goto considered helpful
Date: Thu, 12 Dec 2024 13:50:20 -0800
Organization: None to speak of
Lines: 102
Message-ID: <87wmg4vaeb.fsf@nosuchdomain.example.com>
References: <vi54e9$3ie0o$1@dont-email.me> <87ldwx10gv.fsf@bsb.me.uk>
	<vimtt4$27vv$1@dont-email.me> <86ser1kgp5.fsf@linuxsc.com>
	<vit69t$1qfgg$1@dont-email.me>
	<87ldwtzlc0.fsf@nosuchdomain.example.com>
	<vitjgg$1tukq$2@dont-email.me> <vj1bss$325uo$1@dont-email.me>
	<vj1h4i$335q1$2@dont-email.me> <vj1mhi$34p7h$1@dont-email.me>
	<vj1prj$35je4$1@dont-email.me> <vj7dup$he7i$1@dont-email.me>
	<vjasuj$17uod$1@dont-email.me> <vjc87h$1apid$1@paganini.bofh.team>
	<vjcbe1$1jns0$1@dont-email.me>
	<87wmg5x4b1.fsf@nosuchdomain.example.com>
	<vjd96b$1pbed$1@dont-email.me>
	<87jzc5wx3s.fsf@nosuchdomain.example.com>
	<vjdde8$1q2to$2@dont-email.me>
	<87frmtwuzq.fsf@nosuchdomain.example.com>
	<20241212144454.00003b83@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Thu, 12 Dec 2024 22:50:21 +0100 (CET)
Injection-Info: dont-email.me; posting-host="fce124256618fa8243377af1ae3d31f3";
	logging-data="3092817"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18php9VcX/vLpsGiynODHW1"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:pGGkGk6kCoAyt46FU/0ROP0JJjg=
	sha1:z8GPn9JiNzNvEogNhtRsV2+lpps=
Bytes: 5746

Michael S <already5chosen@yahoo.com> writes:
> On Wed, 11 Dec 2024 17:27:53 -0800
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> bart <bc@freeuk.com> writes:
>> [...]
>> > My experience of multi-level break is that there are two main
>> > use-cases:
>> >
>> >  * Used in the current loop only (not necessarily the innermost to
>> > an observer). This is the most common
>> >
>> >  * Used to exit the outermost loop
>> >
>> > So to support these, named or even numbered loops are not
>> > necessary. (Eg. I use 'exit' or 'exit all'.)  
>> 
>> I would oppose a change to C that only applied to innermost and
>> outermost loops.  For one thing, I'm not aware of any other language
>> that does this (except perhaps your unnamed one).  For another,
>> it's easy enough to define a feature that handles any arbitrary
>> nesting levels, by applying names (labels) to loops.
>
> The better solution is education.
> Convince teachers in unis and colleges that goto is *not* considered
> harmful for this particular use case. Convince them to teach that 
> attempts to avoid goto [for this particular use case] are really
> considered harmful. If you don't believe in authority of yourself then
> ask for help from somebody famous that share this view. I would guess
> that nearly all famous C programmers share it.

Backward gotos tend to be dangerous.  Forward gotos are less so.

Dijkstras original "Go To Statement Considered Harmful" letter was
written in 1968, at a time when many languages didn't necessarily
have the structured control constructs we've come to expect since
then.  Using goto to implement a loop is almost always a bad idea,
but it's something that a modern C programmer probably wouldn't
even consider doing.

I agree that gotos have valid uses.

Here's an answer I wrote on Stack Exchange some years ago to the
question "Is using goto ever worthwhile?" :
https://softwareengineering.stackexchange.com/a/133523/33478

Quoting from that answer :

    The main use of a goto in a reasonably modern language (one that
    supports if/else and loops) is to simulate a control flow construct
    that's missing from the language.

I would support adding named loops and labeled exit/continue to
a future version of C.  I've used languages that have similar
features, and have found them very useful.  Given that C doesn't
have multi-level break, I tend to agree that a goto statement is a
reasonable way to simulate it, often better than the alternatives.
(It's important to use a meaningful label name.)  Similarly,
the C code in the Linux kernel makes extensive use of gotos for
error handling.

If C didn't have a break statement at all, it could be simulated
with goto.  If that were the case, I'd still favor adding a break
statement to the language.  I support adding labeled break statements
for the same reason.  Goto is not the root of all evil, but it's
worth some effort to avoid it when other constructs are clearer.

Here's an example of a small C program that completely avoids the
use of goto statements.  I reserve the right to ridicule anyone who
takes this program seriously.

#include <stdio.h>
#include <setjmp.h>
int main(void) {
    jmp_buf jb[7];
    volatile int j = 0;
    setjmp(jb[0]);
    volatile int i = 1;
    if (!j) setjmp(jb[1]);
    j && i > 100 && (longjmp(jb[6], 0), 0);
    j && !(i % 15) && (longjmp(jb[4], 0), 0);
    j && !(i % 3) && (longjmp(jb[2], 0), 0);
    j && !(i % 5) && (longjmp(jb[3], 0), 0);
    j && printf("%d\n", i);
    j && (longjmp(jb[5], 0), 0);
    if (!j) setjmp(jb[2]);
    j && puts("Fizz");
    j && (longjmp(jb[5], 0), 0);
    if (!j) setjmp(jb[3]);
    j && puts("Buzz");
    j && (longjmp(jb[5], 0), 0);
    if (!j) setjmp(jb[4]);
    j && puts("FizzBuzz");
    if (!j) setjmp(jb[5]);
    i ++;
    j && (longjmp(jb[1], 0), 0);
    if (!j) setjmp(jb[6]);
    j++ < 1 && (longjmp(jb[0], 0), 0);
}

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */