Mail Archives: djgpp/1998/03/24/00:38:42
D. Huizenga wrote:
> A small note:
> Besides having the advantage of
> being able to access the variable outside of the
> loop, according to a previouse thread in this group
> the second way has the added advantage of being
> much faster than the first. If I remember coorectly,
> this is because the variable is declared only once,
> opposed to being declared newly each time the code
> goes back to the top of the loop.
>
Well, I think that would be true if the code was changed to:
for(int k = 0;...;...)
for(int i = 0;...;...)
{
//i has scope here
}
Otherwise as I see it, it would "forget" which iteration it was on.
That is, if i was re-initailized every time in the original code then
how would it be incremented? How could you check the value? I don't
know what the style gurus would say about for(int i = 0;...;...), but I
think it is just bad. I've also heard their is some problems with that
syntax with certain compilers. And if your code isn't as portable as it
could be then you are defeating the purpose of C/C++, IMHO. I wish it
was possible to REASONABLY add VESA or some other graphics standard to
C/C++ so that porting programs that use graphics would be easier. It
would be nice if more API's were in the standard, but you can't have
everything I guess.
> > The new C++ standard decrees that scoped variables are local only to
> > their scope. i.e.,
> >
> > for (int i = 0; ...; ...)
> > {
> > // i has scope here
> > }
> >
> > i = 0; // i does not have scope here; this is illegal
> >
- Raw text -