Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162395
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: I have a confession to make... |
| Date | 2021-08-16 01:50 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87a6liw6v2.fsf@bsb.me.uk> (permalink) |
| References | <sfc03t$r91$1@dont-email.me> <87v946gz7t.fsf@nosuchdomain.example.com> <c0350a6e-422a-47c4-aa97-a054b581c102n@googlegroups.com> |
Thiago Adams <thiago.adams@gmail.com> writes:
> On Sunday, August 15, 2021 at 6:44:35 PM UTC-3, Keith Thompson wrote:
>> Vir Campestris <vir.cam...@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.).
>>
>
> I miss some jumps in C and I am using macros just to give gotos a more
> structured meaning.
>
> #define try if (1)
> #define catch else catch_label:
> #define throw goto catch_label
>
> try {
> if (error) throw;
>
> //success
> }
> catch {
> //error situation
> }
I don't think hiding a goto in a macro does anything to ameliorate the
potential problems. It seems to me it's just going to make things
worse.
--
Ben.
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