Jump to content

  • Log In with Google      Sign In   
  • Create Account


VS2012 Declarations in Loops


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Endemoniada   Members   -  Reputation: 312

Like
0Likes
Like

Posted 14 July 2013 - 04:50 PM

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.

 



Sponsor:

#2 MarkS   Prime Members   -  Reputation: 882

Like
3Likes
Like

Posted 14 July 2013 - 04:56 PM

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.

#3 GeneralQuery   Crossbones+   -  Reputation: 1263

Like
0Likes
Like

Posted 14 July 2013 - 04:56 PM

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.


Edited by GeneralQuery, 14 July 2013 - 05:00 PM.


#4 SimonForsman   Crossbones+   -  Reputation: 6061

Like
3Likes
Like

Posted 14 July 2013 - 07:37 PM

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.


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!

#5 Endemoniada   Members   -  Reputation: 312

Like
0Likes
Like

Posted 14 July 2013 - 09:12 PM

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.



#6 SimonForsman   Crossbones+   -  Reputation: 6061

Like
1Likes
Like

Posted 14 July 2013 - 09:26 PM

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).


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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS