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 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: <87ldwx10gv.fsf@bsb.me.uk> <86ser1kgp5.fsf@linuxsc.com> <87ldwtzlc0.fsf@nosuchdomain.example.com> <87wmg5x4b1.fsf@nosuchdomain.example.com> <87jzc5wx3s.fsf@nosuchdomain.example.com> <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 writes: > On Wed, 11 Dec 2024 17:27:53 -0800 > Keith Thompson wrote: >> bart 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 #include 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 */