Deutsch English Français Italiano |
<vtof09$2db8v$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Loops (was Re: do { quit; } else { }) Date: Wed, 16 Apr 2025 15:31:38 +0100 Organization: A noiseless patient Spider Lines: 87 Message-ID: <vtof09$2db8v$1@dont-email.me> References: <vspbjh$8dvd$1@dont-email.me> <vtc7mp$2q5hr$1@dont-email.me> <vtcqf6$3j95s$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> <20250415053852.166@kylheku.com> <vtm4ae$6d5j$1@dont-email.me> <H7yLP.2056536$OrR5.1414451@fx18.iad> <vtmgj8$g81k$1@dont-email.me> <vtnfjj$1gk91$1@dont-email.me> <vto4fu$23kmr$1@dont-email.me> <20250416150837.00004587@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 16 Apr 2025 16:31:38 +0200 (CEST) Injection-Info: dont-email.me; posting-host="507c987f563baeac8cb4b74b82477869"; logging-data="2534687"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FoNWPFJMCCO/9jS58Sp+b" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:yt8qcajTgSiKIEGNhGoKAQKs4UU= In-Reply-To: <20250416150837.00004587@yahoo.com> Content-Language: en-GB Bytes: 5084 On 16/04/2025 13:08, Michael S wrote: > On Wed, 16 Apr 2025 12:32:13 +0100 > bart <bc@freeuk.com> wrote: > >> >> But never, mind, C's for-loop will still be the most superior to >> everybody here. I'd have an easier time arguing about religion! >> > > Who exactly said that it is superior? Surely not me. > I think, most posters here would agree with my stance that C for() is > non-ideal. esp. for writer, but good enough. > > And it has a minor advantage of being more clear for casual readers > than most "proper" counting loop. Well, there is less ambiguity about the iteration range. But this is something that C's 0-based nature has helped create (see below). > When counting loop is written as C > for() loop, a casual reader does not have to consult the manual about > meaning of the 1st, 2nd and 3rd (if present) parameters. No, you have to analyse the loop instead: for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){ for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){ for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} The first challenge is to isolate the condition! That means carefully counting those ; and , characters. Then figuring out what's going on. These three clearly are not simple iterations, yet they are all start 'for'. I'd write the third (at least) like this: i = 0; while (zLine[i] && IsSpace(zLine[i]) {++i} Now constrast with a language where you KNOW that 'for' isn't going to be doing anything whacky. You know there will be a loop index name and some limits, or a collection of values to loop over: for index in firstvalue..lastvalue for x in collection > I don't know about you, may be your memory is perfect. Mine is not. > Even with python, a language that I use more often than once per year, > remembering whether range(3) means (0,1,2) or (0,1,2,3) is an effort. > Much more so with (modern) Fortran, that I read very rarely. In case of > Fortran, it certainly does not help that the principle of do loop is the > same as for loop in Matlab/Octave that I use regularly, but the order of > parameters differs. When languages used to be 1-based, it was easy. Then 0-based came along, and typically iteration changed from 1..N inclusive, to 0..N-1 inclusive. Now, a range may or may not be inclusive. In Python, 0-based, it isn't, so range(10) or range(0,10) means 0..9 inclusive. > Oh, now you could interpret a written above as statement of superiority > of C syntax. So, no, it is not. Those are *minor* points. Here's some C code to print the elements of an array: static char* table[] = {"one", "two", "three", "four", "five"}; for (int i=0; i<sizeof(table)/sizeof(table[0]); ++i) printf("%d %s\n", i, table[i]); And here is how I'd write it using the 'minor' advantages of my syntax: static []ichar table = ("one", "two", "three", "four", "five") for i, s in table do println i, s end To me, the differences in that for-loop are like chalk and cheese. (Note that the latter is 1-based, but it is not apparent in the code; the outputs are numbered 1 to 5. The C is zero-based, and that has to be specified. The outputs are numbered 0 to 4. Moreover, I could have specified a different array base, and the loop remains unchanged.)