Xref: utzoo comp.lang.modula2:1218 comp.lang.c:15827 Path: utzoo!attcan!uunet!lll-winken!ncis.llnl.gov!helios.ee.lbl.gov!pasteur!agate!eris.berkeley.edu!bowles From: bowles@eris.berkeley.edu (Jeff A. Bowles) Newsgroups: comp.lang.modula2,comp.lang.c Subject: Re: "for" loops (was Re: C++ vs. Modula2) Message-ID: <19579@agate.BERKELEY.EDU> Date: 27 Jan 89 16:18:57 GMT References: <739@jupiter.iis.UUCP> <1611@csuna.UUCP> <738@atanasoff.cs.iastate.edu> Sender: usenet@agate.BERKELEY.EDU Organization: University of California, Berkeley Lines: 41 In article <738@atanasoff.cs.iastate.edu> hascall@atanasoff.cs.iastate.edu (John Hascall) writes: >In article <739@jupiter.iis.UUCP> heiser@iis.ethz.ch (Gernot Heiser) writes: > >>worst features. C's 'for' is really a 'while' with an initialization clause and >>a designated spot that may contain an incrementation clause. What I consider a >>"real" 'for' (as opposed to the while/repeat/loop family) is a construct that >>executes a specific number ot times, the iteration count being determined >>BEFORE the processing of the body starts.... I hate to fan flames like this, but I can't resist. I have strong reservations about certain things in C, but the "for" loop is something that's really kinda nice: 1. It's not restrictive on the types of the indices, because it defines a more general construct. If I had a nickel for every time, in Fortran, that I needed a loop that ran from 0.0 to 1.0 by 0.1 (or the like) and had to use INTEGER to do it. Or Pascal, which lacked the "step" clause so that you couldn't increment by more than what the language-designer wanted. 2. Yes, you're right, it's redundant - the "while (expr) statement;" and the "do statement; while (expr)" and the "for (expr;expr;expr) statement;" have a lot of redundancy, probably for brevity. So? 3. Because it's not restrictive on the types, and because I don't have to know EXACTLY how many times it will run through the body, I can do things like: for (p = headoflist; p != NULL; p = p->l_next) process(p); The article said that you could code loops that run a arbitrary number of times, using something like: for (i = 0; i < thingwithsideeffects(i); i++) munge(); And while the author of the article is correct, you can code garbage like this in most languages. Fault the coder, in this case. The only thing I really miss is something you Unix-types will recognize from awk (and perhaps from Algol 68?) - for (t in table) process(table[t]); But that's another story.... Jeff Bowles