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

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

Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Loops (was Re: do { quit; } else { })
Date: Sat, 19 Apr 2025 13:55:02 -0700
Organization: None to speak of
Lines: 56
Message-ID: <87ldrvna6x.fsf@nosuchdomain.example.com>
References: <vspbjh$8dvd$1@dont-email.me> <vtdh4q$b3kt$1@dont-email.me>
	<vtf7fe$1qtpg$1@dont-email.me> <vtgfuf$31ug1$1@dont-email.me>
	<20250413072027.219@kylheku.com> <vtgpce$39229$1@dont-email.me>
	<vti2ki$g23v$1@dont-email.me> <vtin99$vu24$1@dont-email.me>
	<vtiuf0$18au8$1@dont-email.me> <vtj97r$1i3v3$1@dont-email.me>
	<vtl166$36p6b$1@dont-email.me> <vtlcg0$3f46a$2@dont-email.me>
	<vtnekn$1fogv$1@dont-email.me> <vto2mb$20c4n$1@dont-email.me>
	<vtu4i5$3hteg$1@dont-email.me> <vtujko$3uida$1@dont-email.me>
	<hxOMP.335104$j2D.272394@fx09.iad> <20250419092849.652@kylheku.com>
	<vu0t5m$22rjp$1@dont-email.me> <vu0v2n$22n7b$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sat, 19 Apr 2025 22:55:03 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b62a019a496a9456e80ffe1b61d2a7b5";
	logging-data="2214692"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18bgWfoXTAymYqUJj4U+mFk"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:MujxiQOEsVl4d8+gUlzHGzMngXA=
	sha1:pm8sVtu06gtOPc0om+8vDcy/Cdc=

bart <bc@freeuk.com> writes:
[...]
> The fact is that people make typos (obviously, not you or JP or SL or
> KT!), and here you would really prefer that the compiler could report
> them, but with this feature, it often can't.

Of course I make typos.

I haven't mentioned this, but I wrote "<=" rather than the correct
"<" in a C for loop just a few days ago, in a tiny test program.
I made, found, and corrected the error within perhaps a minute.
There was no diagnostic from the compiler.  It would have been nice,
I suppose, if there had been.

Having compilers recognize certain patterns in for loops and issuing
warnings for things that appear to be errors is not a bad idea.
For example, if someone writes :

    for (int i = 0; i <= N; j ++)

an optional warning would not be unreasonable.  (A fatal error would
be unreasonable, since the code might be correct, though probably
too terse.)  Note that this error will be flagged unless there
happens to be a "j" in scope, but that could happen in a nested loop.

Likewise if someone writes :

    int arr[5];
    for (int i = 0; i <= sizeof arr / sizeof arr[0]; i ++)

with a reference to arr[i] in the body of the loop, a warning would
be reasonable.  And in fact gcc does issue a warning with "-Wextra"
and "-O1" or higher: "warning: iteration 5 invokes undefined behavior
[-Waggressive-loop-optimizations]".

C-style for loops do introduce the risk of some errors that
are less likely to occur with less flexible forms of for loops.
Speaking for myself, that is (a) a price I'm willing to pay in
return for increased flexibility, (b) manageable by writing code
carefully, which includes following common idioms, and (c) not
something I can do anything about given that the language isn't
going to change any time soon.

Again, I would not object to adding a new kind of for loop,
similar to what you would prefer, and visually distinct from the
existing for loop, in a new version of the C standard.  But that's
not likely to happen because there doesn't seem to be much demand
for it (for reasons that I know make you angry), and I don't care
enough to write a proposal.  If someone else does write a proposal,
I'll be glad to help out by nitpicking it.

[...]

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