You can do what is shown in both of your examples, but you shouldn't. The first is a very bad practice, while the second depends on how it is used.
In the first case, it's called creating a shadow variable. It is legal and possible, but almost always causes problems and leads to bugs.
A shadow variable is one that is created with the same name as an earlier variable with greater scope. In your first example, the n
on line 6 is the shadow variable, shadowing the earlier n
. It will mask or suspend the earlier variable while it exists. The shadow var exists while it is in scope, i.e., inside the closest pair of curly braces or parentheses that contain it. Once it goes out of scope or ceases to exist, the original var reappears to the code with whatever value it contained before being masked.
In the second example, your interpretation is correct. This practice can be either good or bad.
If the var is being used to carry a value forward from one pass through the loop to the next, the var MUST be declared before the start of the loop so that it will maintain scope and continue to exist between passes.
If the loop doesn't need to preserve data between passes, then it can be declared inside the loop and it will work, but it's still a bad practice. It's arguably better to create it before the loop and reset it's value inside the loop on each pass when needed.
Here's the argument. On one hand, declaring it inside the loop guarantees that it will go out of scope at the end of each pass and be destroyed, then recreated on the next pass. This assures that no data from the previous pass will make it's way to the next pass. This is fine when performance isn't an issue. (The program is running once, the loop is executed a small number of times in terms of computing - executing time is measured in minutes, seconds, or less. )
On the other hand, what if it's a production program that runs millions of times or has millons of copies running in parallel on the same computer. Think about an airline ticket program or Amazon.com! ANYTHING that takes extra time can have a massive impact on execution time! Now, it's far more important to look at efficiencies like this!
So, in one of our simple psets, redeclaring in the loop can be fine, but it might not be the best practice in a production environment. It's up to the programmer to decide. ;-)
If you have any questions, add them as comments.
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)