C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 30th, 2004, 10:59 AM
Spulwurm Spulwurm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 Spulwurm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
declaring variables in or before loops?

Hi.
I'm new to C++ and I've been wondering whether there is a great difference between declaring a variable directly inside a for loop (case 1) or before the loop and altering it inside the loop (case 2).

case 1:
Code:
for (int i=0; i<10; ++i){
     float r=10.0*i;
     cout<<r<<endl;
}

case 2:
Code:
float r;   
for (int i=0; i<10; ++i){
       r=10.0*i;
       cout<<r<<endl;
}   


Will this mean that in case 1 the variable is declared and initialized 10 times in that loop? (and discarded at the end of it?)
and will this cost more time than case 2?
(not that it matters in this example )

Thanks for your help,
Spulwurm

Reply With Quote
  #2  
Old September 30th, 2004, 02:00 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 16
Its best to declare variables with as specific scope as possible in order to help readability and maintainability and leave it up to the compiler to sort out optimizing it. Any compiler worth its salt will sort things like this out and do the 'right' thing.

-KM-

Reply With Quote
  #3  
Old October 1st, 2004, 06:03 AM
Spulwurm Spulwurm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 Spulwurm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by kode_monkey
Any compiler worth its salt will sort things like this out and do the 'right' thing.


I had hoped for that!

Reply With Quote
  #4  
Old February 11th, 2011, 12:37 PM
solod solod is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 6 solod User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 41 sec
Reputation Power: 0
Quote:
Originally Posted by Spulwurm
Hi.
I'm new to C++ and I've been wondering whether there is a great difference between declaring a variable directly inside a for loop (case 1) or before the loop and altering it inside the loop (case 2).

case 1:
Code:
for (int i=0; i<10; ++i){
     float r=10.0*i;
     cout<<r<<endl;
}

case 2:
Code:
float r;   
for (int i=0; i<10; ++i){
       r=10.0*i;
       cout<<r<<endl;
}   


Will this mean that in case 1 the variable is declared and initialized 10 times in that loop? (and discarded at the end of it?)
and will this cost more time than case 2?
(not that it matters in this example )

Thanks for your help,
Spulwurm

so for your question, it doesn't really matter where you declare your variable, as long as you declare it. mostly it is personal preference. however, most people prefer for variables to be declared out of the loop for readability purposes.even like this, the variable is still dclared out of its designated loop.
Code:
float r;   
for (int i=0; i<10; ++i){
       r=10.0*i;
       cout<<r<<endl;
float x;   
for (int i=0; i<10; ++i){
       X=10.0*i;
       cout<<x<<endl;}
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > declaring variables in or before loops?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap