Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162391
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: I have a confession to make... |
| Date | 2021-08-15 14:44 -0700 |
| Organization | None to speak of |
| Message-ID | <87v946gz7t.fsf@nosuchdomain.example.com> (permalink) |
| References | <sfc03t$r91$1@dont-email.me> |
Vir Campestris <vir.campestris@invalid.invalid> writes:
> I've been writing C, and more lately C++, since some time in the 1980s.
>
> I've just written a goto. I know it's evil, and I shouldn't do it, but
> just this once...
>
> And it isn't in any of the serious stuff I do in may day job either.
>
> You all know what a magic square is - a square where all the columns,
> rows, and diagonals add up to the same value.
>
> I'm messing about doing it for a cube. Wikipedia says you can't for
> small cubes.
>
> I have a square for the base, and a centre value, and therefore I can
> work out all the values for the top square as they are on diagonals
> with one of the bottom values and the centre one.
>
> I therefore have 3 nested loops: centre value, X and Y coordinates.
>
> And if I don't like the result I jump out of the X Y loops. Which is
> where I have that goto.
Breaking out of a nested loop is one legitimate use case for a goto. An
alternative is to separate the inner loop into another function and use
return rather than goto. That might not be practical. And it's always
*possible* to write without gotos (there's a theorem to that effect),
but sometimes at the cost of making the code more difficult to read.
As I wrote here:
https://softwareengineering.stackexchange.com/a/133523/33478
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'd like to see an extended "break" (and "continue") that refers to the
name of the loop to be broken -- note: not the code location to which it
jumps, but the name of the construct that's being affected. A number
of other languages have this kind of named break (Ada, Perl, etc.).
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
I have a confession to make... Vir Campestris <vir.campestris@invalid.invalid> - 2021-08-15 22:14 +0100
Re: I have a confession to make... Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-15 14:44 -0700
Re: I have a confession to make... Thiago Adams <thiago.adams@gmail.com> - 2021-08-15 15:56 -0700
Re: I have a confession to make... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-08-16 01:50 +0100
Re: I have a confession to make... Bart <bc@freeuk.com> - 2021-08-16 00:18 +0100
Re: I have a confession to make... DFS <nospam@dfs.com> - 2021-08-15 20:31 -0400
Re: I have a confession to make... David Brown <david.brown@hesbynett.no> - 2021-08-16 08:20 +0200
Re: I have a confession to make... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-08-15 23:21 -0700
Re: I have a confession to make... Vir Campestris <vir.campestris@invalid.invalid> - 2021-08-17 21:43 +0100
Re: I have a confession to make... Mark Bluemel <mark.bluemel@gmail.com> - 2021-08-16 00:32 -0700
Re: I have a confession to make... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-08-16 14:21 +0100
Re: I have a confession to make... Bart <bc@freeuk.com> - 2021-08-16 17:48 +0100
Re: I have a confession to make... Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-09-06 06:30 -0700
Re: I have a confession to make... David Brown <david.brown@hesbynett.no> - 2021-09-06 18:04 +0200
csiph-web