Article View: comp.lang.c
Article #585Re: Taming The Switch-Statement
From: Keith Thompson
Date: Tue, 20 Oct 2026 00:03
Date: Tue, 20 Oct 2026 00:03
66 lines
4393 bytes
4393 bytes
Bob Duff <invalid@invalid.com> writes:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>> Names used for loops have a lighter-weight syntax:
>>
>> LOOP_NAME:
>> while condition loop
>> -- ...
>> end loop;
>
> That's a syntax error. In Ada, if you put a loop name at the top of the
> loop, you are required to repeat it at the bottom, like this:
>
> LOOP_NAME: while condition loop
> ...
> end loop LOOP_NAME;
Right, I forgot that. It's also a syntax error because there are no
statements in the body of the loop, just a comment.
> If there's an "exit LOOP_NAME;" in there, it's pretty clear which
> loop it's jumping out of, AND where it's jumping to.
Yes.
>> I can't think of a better syntax for loop names than "NAME:" (used
>> by several other languages), but in C that syntax is already
>> taken for goto labels.
>
> Agreed.
>
> But in current C (before adding the named-loop feature),
> and in Ada, and in many other languages, a label serves
> as a warning: One or more gotos jump here. That's useful
> information.
Well, zero or more gotos, but yes.
> This new feature of C seems to lose that benefit, which
> is unfortunate. But I think the "while LOOP_NAME..."
> syntax is less readable than "LOOP_NAME: while...",
> as you said.
Agreed -- but you can always search a function body for occurrences
of "goto". If a function is big enough to make that difficult,
either it's machine-generated or you should probably rethink your
program structure.
Once the new feature is widely available, I think that the fact that
a label is at the top of a loop or switch statement is a strong clue
that it's *not* the target of a goto.
> So if it were up to me, I wouldn't add named loops to C in
> either proposed syntax. The "goto" is good enough for
> jumping out of nested loops.
I commonly use named loops in Perl -- and in a recent search through
a bunch of my Perl code, I found that I typically *don't* use it as
a way to break out of a nested loop. I find that giving a name to a
loop can make the code easier to understand: this loop iterates over
command-line arguments, that one iterates over lines in a file. (Perl
uses the same syntax for goto labels and loop names. I don't think
I've ever used goto in Perl outside small throwaway test scripts.)
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Technical Details
Message-ID:
<11b6b4i$2fcat$5@kst.eternal-september.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <20261010230355.00005189@yahoo.com> <11ae7eb$cjlv$6@dont-email.me> <11aeee0$24k8m$1@kst.eternal-september.org> <11aer17$286ml$2@dont-email.me> <11aeusp$29hmu$2@kst.eternal-september.org> <86mrsdpcgp.fsf@linuxsc.com> <11au6pr$3mdne$1@kst.eternal-september.org> <11auc0s$206gh$3@dont-email.me> <11b285v$121v2$2@dont-email.me> <11b293j$12jfa$1@dont-email.me> <11b2cuh$141vj$1@dont-email.me> <11b2eck$3pvd9$1@dont-email.me> <20261018171339.00005308@yahoo.com> <11b2mf1$16mof$2@dont-email.me> <20261018181623.00007978@yahoo.com> <11b347h$1cn63$1@dont-email.me> <20261018223859.000031d3@yahoo.com> <11b4n4s$1q18k$1@dont-email.me> <11b4smj$1vt4q$1@kst.eternal-september.org> <wccecdl8dmp.fsf@shell02.theworld.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Mon, 19 Oct 2026 17:03:30 -0700 Organization: None to speak of Lines: 66 Message-ID: <11b6b4i$2fcat$5@kst.eternal-september.org> References: <119qf6l$2rmg3$1@dont-email.me> <20261010230355.00005189@yahoo.com> <11ae7eb$cjlv$6@dont-email.me> <11aeee0$24k8m$1@kst.eternal-september.org> <11aer17$286ml$2@dont-email.me> <11aeusp$29hmu$2@kst.eternal-september.org> <86mrsdpcgp.fsf@linuxsc.com> <11au6pr$3mdne$1@kst.eternal-september.org> <11auc0s$206gh$3@dont-email.me> <11b285v$121v2$2@dont-email.me> <11b293j$12jfa$1@dont-email.me> <11b2cuh$141vj$1@dont-email.me> <11b2eck$3pvd9$1@dont-email.me> <20261018171339.00005308@yahoo.com> <11b2mf1$16mof$2@dont-email.me> <20261018181623.00007978@yahoo.com> <11b347h$1cn63$1@dont-email.me> <20261018223859.000031d3@yahoo.com> <11b4n4s$1q18k$1@dont-email.me> <11b4smj$1vt4q$1@kst.eternal-september.org> <wccecdl8dmp.fsf@shell02.theworld.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Tue, 20 Oct 2026 00:03:31 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="2601309"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19CQxHujog33WEwZyr5F6WE"; posting-host="8b71722c3d75191180c81c99be85908a" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:USPEMSOKnmpVtSekK3iTlP/ZqNQ= sha256:ZVT8nxNR8+02GQ7pBS3OIjVK1x48N/1iQD4mV4ra9uY= sha1:J+uQlIDjH2n2BUIkw4f1KnZFMHw= sha256:Zii2yQMDrXTTSw0VBIN1b1TqRGd20NMBWbv+HSlOVYk=