Sujet : Checking the loop variable after the loop has ended (Was: Loops (was Re: do { quit; } else { }))
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : comp.lang.cDate : 17. Apr 2025, 22:21:54
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <vtrrdi$1smfe$1@news.xmission.com>
References : 1 2 3 4
User-Agent : trn 4.0-test77 (Sep 1, 2010)
In article <
87fri68w2c.fsf@nosuchdomain.example.com>,
Keith Thompson <Keith.S.Thompson+
u@gmail.com> wrote:
...
IMHO it doesn't much matter what the value is after the loop ends, but
any standard for a language with such a feature should either restrict
the scope to the loop, specify the value the variable has after the
loop, or explicitly say that it's unspecified or undefined.
I frequently check the value of the loop variable after the loop has ended
in order to determine if the loop ended "normally" or prematurely via
"break". E.g.,
for (i=0; i<10; i++) { code that might or might not break }
if (i == 10) puts("It ended normally");
I've applied this method in many C and (vaguely) C-like languages.
Any language with a "for" type loop, where you can check the value after
the loop can avail themselves of this method.
-- The motto of the GOP "base": You can't *be* a billionaire, but at least youcan vote like one.