From a theoretical design point of view, you should make your variables accessable only where they are needed. If they are needed only in a particular section of code, then only that section of code should be allowed to access them.
It just so happens this also tends to result in better and more bug-resistant code. If code that doesn't need the variable, can't get at the variable, then there's no chance it might screw up with the variable.
I'm not too savy on any figures about whether it is faster or not, but from a design point of view, if the variables are only required *inside* the for loop, then declare it only inside the loop. The problem with that, though, is it will reset the variable each time the loop is ran, in which case you have no choice but to declare the variable just before the loop.
In this example we go through some bank accounts and find out how much money they have in another currency if the exchange rate is "rate". You could just store the calculation directly into totalmoney but for point of simple example, let's not.
The convertedmoney variable does not exist outside the loop and attempting to access it will make the compiler complain the variable doesn't exist (on strict mode at least!) Which is good, because it doesn't make sense for the convertedmoney variable to continue existing after the loop. Its job is done. Thus potential errors are removed, and possibly the loop will run faster.
It just so happens this also tends to result in better and more bug-resistant code. If code that doesn't need the variable, can't get at the variable, then there's no chance it might screw up with the variable.
I'm not too savy on any figures about whether it is faster or not, but from a design point of view, if the variables are only required *inside* the for loop, then declare it only inside the loop. The problem with that, though, is it will reset the variable each time the loop is ran, in which case you have no choice but to declare the variable just before the loop.
In this example we go through some bank accounts and find out how much money they have in another currency if the exchange rate is "rate". You could just store the calculation directly into totalmoney but for point of simple example, let's not.
Local totalmoney:Int = 0 For Local tempaccount:TAccount = EachIn accountlist Local convertedmoney:Int = tempaccount.money * rate totalmoney :+ convertedmoney Next
The convertedmoney variable does not exist outside the loop and attempting to access it will make the compiler complain the variable doesn't exist (on strict mode at least!) Which is good, because it doesn't make sense for the convertedmoney variable to continue existing after the loop. Its job is done. Thus potential errors are removed, and possibly the loop will run faster.