Topic: For loop variable definitions


Author: pjl@graceland.att.com (Paul J. Lucas)
Date: Tue, 17 Jan 1995 15:42:49 GMT
Raw View
In <reidar.husmo.23.2F1B93C4@hiof.no> reidar.husmo@hiof.no (REIDAR HUSMO) writes:

>In C, I'd write something like int i,j; for(i=0,j=0 ; ....
>In C++, I'd *like* to write something like:
> for (int i=0, float x=0.0; ...
>Needless to say, if this had worked, I wouldn't be posting.
>There is, of course, a confilict with normal variable definitions here,
>as the comma normally indicates that there are more vars to come
>which are of the same type.

>So. Does anybody else think that it should it work?

 I don't.

>Is there a workaround with clever use of parenthesis/brackets/braces that
>I haven't thought of?

 Just put them outside.  The feature of allowing declarations
 inside is a mere nicety; if the semantics of the current rule
 don't suit you, just put them outside.

 The ANSI committee has changed the rule once; it won't change it
 again.
--
 - Paul J. Lucas   #ifndef COMMON_KNOWLEDGE
   AT&T Bell Laboratories #include <stddisclaimer.h>
   Naperville, IL  #endif




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 17 Jan 1995 17:42:44 GMT
Raw View
In article 2F1B93C4@hiof.no, reidar.husmo@hiof.no (REIDAR HUSMO) writes:
>
>In C++, I'd *like* to write something like:
> for (int i=0, float x=0.0; ...

The code
 int i=0, floatx=0.0
is not a valid declaration or expression, so why would you expect to be able
to write it in a for loop header?

The only change in the for loop was to allow any statement, including
a declaration statement, to appear as the first expression. No new
syntax was invented.

---
Steve Clamage, stephen.clamage@eng.sun.com






Author: osinski@valis.cs.nyu.edu (Ed Osinski)
Date: 19 Jan 1995 21:41:00 GMT
Raw View
In article <3fhnju$qsn@engnews2.Eng.Sun.COM>, landauer@morocco.Eng.Sun.COM (Doug Landauer) writes:
|> > The only change in the for loop was to allow any statement, including
|> > a declaration statement, to appear as the first expression. No new
|> > syntax was invented...
|>
|>  ... except for the new "condition-declarations" which allow
|> (initialized) variables to be declared in the condition parts of "if",
|> "switch", "while", and, you guessed it, "for" loops.  So now you can say
|>
|>  for( int i= 0; int j= f(i); ++i ) {
|>      // Do stuff with i and j.  The test-part fails when
|>      // f(i) returns a 0.
|>  }
|>  // Out here, "i" is now out of scope.  (Recent change.)
|>
|> if you want to be exceptionally obtuse.  Few compilers allow this yet.
|>
|> The other fairly recent change, which Paul Lucas might've been referring
|> to but which the original poster didn't run into yet, is the scope change:
|> variables declared in the init-section of a "for" loop now go out of scope
|> upon the end of the "for" statement.
|>
|> Seems to me that the end result of that change is that people are (for
|> awhile) less likely to declare variables in the for-loop init section,
|> since they're likely to want their code to work with most current
|> compilers, but still work once most compilers have implemented the scope
|> change.  [People who care about writing portable code, anyway.]

I like the new rule (regarding scope), but don't want to wait for the compilers
I use to implement it (g++ and cfront), so I use this handy, dandy macro
suggested by someone (I don't remember who) on the net:

#define for    if (0) ; else for


|> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|> Doug.Landauer@sun.com    || "C++ is sometimes more subtle than it appears at
|>  SUNW[SunSoft]->DevPro:: ||  first." -- "Understatement of the Year" award
|>   Languages.ADE(C++);    ||    winner Jerry Schwarz

--
---------------------------------------------------------------------
 Ed Osinski                  |
 Computer Science Department | "Do I know you?
 New York University         |  And don't try to deny it!"
 E-mail:  osinski@cs.nyu.edu |                 Col. Flagg to Hawkeye
---------------------------------------------------------------------