Sujet : Re: on allowing "int a" definition everywhere
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 23. Aug 2024, 12:25:32
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <va9rjc$t0r0$2@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 23/08/2024 11:47, fir wrote:
Bart wrote:
btw maybe not so much relevent as what you write but
if to think the convention
foir(int i=0; i<100; i++)
{
//,,,
}
to amke int i scope relevant to only inner of the loop seem just logically wrong
Actually it's one of the few places it makes sense!
But I don't like this idiom for several reasons. Sure, it can be convenient to write:
for(int i=0; i<100; i++)
without having to make an annoying detour to the top of the function to write that declaration for i. But then you need a second loop, and a third, and how you have to repeat a declaration each time:
for(int i=0; i<200; i++)
Better to do it once and forget about it.
Then, it allows nested loops like this:
for (int i = 0; i<A; ++i)
for (int i = 0; i<B; ++i)
for (int i = 0; i<C; ++i)
All those i's are different! Only the last is accessible in the inner loop.