dm@phsbbs.princeton.nj.us (Doron Meyer) (06/11/91)
Hello net land. What is the 'preferred' stylistic method of incrementing counters? For example, this simple for loop. for (count=0; count<4; count++) { printf("name[%d]: %c\n", count, ptr++); In this instance, the loop counter is incremented in the declaration of the for loop, and the ptr variable, which still gets incremented each time the loop cycles, is incremented within the body of the loop. Is this more of a matter of personal opinion than anything else? If so, what are your opinions? ----- Doron Meyer, System Administrator Emeritus, Princeton High School DNS: dm@phsbbs.princeton.nj.us UUCP: ...!princeton!phsbbs!dm PSTN: 609-921-2978 USnail: 64 Farrand Road / Princeton, NJ 08540
wollman@emily.uvm.edu (Garrett Wollman) (06/14/91)
[The original poster asks about style wrt for(init; cond; index++) { some_other_var++; other_stuff(); } versus for(init; cond; index++,some_other_var++) { other_stuff(); ] In general, I think that there are two types of situations here. For example, I might find myself doing something like: for(i=0,j=0; i<imax && i<jmax; i++) if(a[i] > 0) b[j++] = a[i]; where clearly, the increment of j cannot be done within the context of the for itself, because it is conditional. However, in many cases, this is not true. Generally, these fall into two categories: either the programmer is trying to hand-optimize the code, or the programmer is trying to express what stylistically ought to be a while loop as a for loop instead. I do not understand why people do that. Perhaps it makes dealing with certain brain-damaged debuggers easier to have for(init; cond; action1,action2,action3); than init; while(cond) { action1; action2; action3; } It's a good obfuscatory technique, but I don't think anyone would disagree with me when I say that the latter is stylistically much better. The other case, that of hand-optimization, is somewhat more difficult. I know that all of the compilers I use can do a much better job of strength-reduction, loop unrolling, CSE, constant code motion, and all the rest, than I can. So in general, instead of doing something like the originally proposed for(i=0; i < imax; i++) printf("%d: %c\n",i,str++); I would instead almost always prefer for(i=0; i < imax; i++) printf("%d: %c\n",i,str[i]); since I am certain that *if* the second form were more efficient than the first *on my specific target machine*, then the compiler would do the conversion automatically, and probably allocate a register for the temporary copy of str as well, not to mention other potential optimizations. I would be surprised if GCC doesn't do this under -O -fstrength-reduce [and a bunch of other -f's all of which IMHO should be enabled by -O automatically], at least for some values of 'target machine'. And, in fact, I find that it does just that (when I substitute a pointer type which is non-trivial) on the NS32532 in this MultiMax; MIPS CC on the SGI 4D/240S does exactly the same thing (comparing -O0 with -O2). So that's my opinion. Perhaps Dan Bernstein will disagree. -GAWollman Garrett A. Wollman - wollman@emily.uvm.edu Disclaimer: I'm not even sure this represents *my* opinion, never mind UVM's, EMBA's, EMBA-CF's, or indeed anyone else's.
mouse@thunder.mcrcim.mcgill.edu (der Mouse) (06/15/91)
In article <XamD43w164w@phsbbs.princeton.nj.us>, dm@phsbbs.princeton.nj.us (Doron Meyer) writes: > What is the 'preferred' stylistic method of incrementing counters? > For example, this simple for loop. > for (count=0; count<4; count++) > { > printf("name[%d]: %c\n", count, ptr++); > In this instance, the loop counter is incremented in the declaration > of the for loop, and the ptr variable, which still gets incremented > each time the loop cycles, is incremented within the body of the > loop. > Is this more of a matter of personal opinion than anything else? Ummm...it's certainly partly pure style. One possible semantic difference occurs when some increments should always happen. This is obvious when they're within if statements or some such, but it may be less obvious when they're skipped due to a continue statement. In the rest of this I assume conditions are such that there's no semantic difference. I would say that the side-effect (increment, decrement, function call, etc) belongs in the for only when it is conceptually part of the loop control. For example, for (pa=a,pb=b;*pa!=*pb;pa++,pb++) is fine, where the loop is conceptually moving two pointers in parallel. But count = 0; for (consp=list;consp;consp=consp->cdr) { count ++; frob_it(consp); } is certainly to be preferred (preferably spaced out more) over for (consp=list,count=0;consp;consp=consp->cdr,count++,frob_it(consp)) ; The line I'm trying to describe here is fuzzy, I'll agree; that's why I say it's partly personal style. der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu
mouse@thunder.mcrcim.mcgill.edu (der Mouse) (06/16/91)
In article <1991Jun14.034527.11467@uvm.edu>, wollman@emily.uvm.edu (Garrett Wollman) writes: > I do not understand why people [put multiple actions in a for loop]. > Perhaps it makes dealing with certain brain-damaged debuggers easier > to have > for(init; cond; action1,action2,action3); > than > init; > while(cond) { > action1; > action2; > action3; > } > It's a good obfuscatory technique, but I don't think anyone would > disagree with me when I say that the latter is stylistically much > better. I would disagree with any such blanket statement. First of all, they have different semantics when a continue statement is present in the body of the loop. Second, when the actions are all directly loop-related, the first is conceptually more coherent, and if they're short, often more readable. (This is not to say that the latter is never preferable; I would disagree equally with *that* blanket assertion.) der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu
Dave.Harris@f14.n15.z1.fidonet.org (Dave Harris) (06/16/91)
>From: dm@phsbbs.princeton.nj.us (Doron Meyer) >Date: 11 Jun 91 12:09:44 GMT >Organization: Princeton High School, Princeton, New Jersey >Message-ID: <XamD43w164w@phsbbs.princeton.nj.us> >Newsgroups: comp.lang.c >Hello net land. What is the 'preferred' stylistic method of incrementing >counters? For example, this simple for loop. >for (count=0; count<4; count++) >{ > printf("name[%d]: %c\n", count, ptr++); you could go: for (count=0 ; count < 4; count++, ptr++) printf("name[%d]: %c\n", count, ptr); I have seen both done but actually tend to use it the way you do. Dave Harris. -- Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!15!14!Dave.Harris Internet: Dave.Harris@f14.n15.z1.fidonet.org