VS2012 Declarations in Loops

Started by
4 comments, last by SimonForsman 10 years, 9 months ago

Hi guys,

In older VS versions I used to be able to do this:

for(int i=0;i<100;i++)
// loop something
 
for(i=0;i<100;i++)
// loop something

Notice how the 'i' isn't declared in the second loop. I can't do that in VS2012.

Is doing the following bad ?

for(int i=0;i<100;i++)
// loop something
 
for(int i=0;i<100;i++)
// loop something
 
for(int i=0;i<100;i++)
// loop something

I have been doing it like this when I have more than one loop:

int i;
 
for(i=0;i<100;i++)
// loop something
 
for(i=0;i<100;i++)
// loop something

...but I'd rather declare it in the loop itself.

Thanks.

Advertisement
The older version of VS was incorrect. The variable should go out of scope after the loop terminates. The second loop should have given you an error. Your second and third methods are correct.

If the counter is only relevant to that loop (seeing as it's being initialized in each loop) then I see no reason why you would want the scope to exceed the loop. What is your rationale behind declaring it outside of the loop(s)?

Edit: Sorry, upon re-reading you state that you do want to declare it for each loop. My bad.

Hi guys,

In older VS versions I used to be able to do this:


for(int i=0;i<100;i++)
// loop something
 
for(i=0;i<100;i++)
// loop something

Notice how the 'i' isn't declared in the second loop. I can't do that in VS2012.

Is doing the following bad ?


for(int i=0;i<100;i++)
// loop something
 
for(int i=0;i<100;i++)
// loop something
 
for(int i=0;i<100;i++)
// loop something

I have been doing it like this when I have more than one loop:


int i;
 
for(i=0;i<100;i++)
// loop something
 
for(i=0;i<100;i++)
// loop something

...but I'd rather declare it in the loop itself.

Thanks.

IIRC the last version of VS that allowed the first version of your code was VS6 , it predates the first C++ standard(c++98) and was, just like all other "C++" compilers at the time using its own unique version of the language(Since there was no standard for the language each compiler vendor had to decide on its own how to handle the details)

Today there are 3 proper versions of C++: C++98, C++03 and C++11, they are different from eachother and code written for C++98 will not always compile with a C++11 compiler.

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

I think it might have been VC6, I feel like all I've ever used before 2012 was VC6.

In any case, and I hope this isn't a dumb question, is it less efficient to do this:

for(int i=0;i<10;i++)
// loop
 
for(int i=0;i<10;i++)
// loop
 
for(int i=0;i<10;i++)
// loop
 
for(int i=0;i<10;i++)
// loop

...than this:

int i;
 
for(i=0;i<10;i++)
// loop
 
for(i=0;i<10;i++)
// loop
 
for(i=0;i<10;i++)
// loop
 
for(i=0;i<10;i++)
// loop

... since the first version has four declarations and the second vesion has only one ?

Thanks.

I think it might have been VC6, I feel like all I've ever used before 2012 was VC6.

In any case, and I hope this isn't a dumb question, is it less efficient to do this:


for(int i=0;i<10;i++)
// loop
 
for(int i=0;i<10;i++)
// loop
 
for(int i=0;i<10;i++)
// loop
 
for(int i=0;i<10;i++)
// loop

...than this:


int i;
 
for(i=0;i<10;i++)
// loop
 
for(i=0;i<10;i++)
// loop
 
for(i=0;i<10;i++)
// loop
 
for(i=0;i<10;i++)
// loop

... since the first version has four declarations and the second vesion has only one ?

Thanks.

normally the compiler should be able to use a register when you declare an integer in the loop, it should be harder for it to optimize it if the variable is declared outside of the loop (since the variable will stay around for longer).

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement