loop

Is it better to instantiate a variable for each instance or outside the loop?
If it matters why.

a) Each instance
for(int i=0;i<30;i++){
   int itemId=.....;
}
b) outside the loop:
int itemId=0;
for(int i=0;i<30;i++){
   itemId=.....;
}
johnkainnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
To the question, "Is it better to instantiate a variable for each instance or outside the loop?", It is just a matter of personal style. The only benefit by using option "B" above is that you have access to the loop control variable outside of the loop if you need it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HooKooDooKuCommented:
Here is my best GUESS (based on my experience with programming, working with complilers, and what I know about assymbly language)...

It doesn't matter apart from scope (i.e. where the variable can be seen/accessed).
The only way it COULD make a difference at runtime is if the variable keeps getting initialized each time the loop is run.  For languages such as VB that ALWAYS initialize a variable for you, each time through the loop would be running extra code to reset the variable value.  But for languages such as C, a variable such as this is created by simply allocating space on the stack at the start of the function.  The initial value of the variable is what ever random data was at that memory location when allocated.  The variable exists during the entire execution of the function, but the compliler limits access to the variable to within the loop.
0
anarki_jimbelSenior DeveloperCommented:
I agree to FernandoSoto - this is a matter of style mainly. However, there are some recommendations:
Variables should be declared as close to where they are used as possible.

In your case - inside the loop (unless you need this variable outside the loop).

See the discussion:
http://stackoverflow.com/questions/318943/where-do-you-declare-variables-the-top-of-a-method-or-when-you-need-them
0
Anurag ThakurTechnical ManagerCommented:
in case of a int or a value type variable declaration and initialization will make much difference for a large object/reference type variables
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.