Title, do you define a new variable within the loop that is not each cycle?
Reply to discussion (solution)
It's hard to say, look at your needs.
It's hard to say, look at your needs.
Each time a local variable is needed in the loop, is the variable defined in the loop body or in the loop body?
This depends on your needs, if you want the variable can change the value, loop to the next time can be used, put on the outside, if only the body of each loop is valid, put inside.
You can put it outside and not put it in there.
You can put it in the outer layer.
Or that sentence, if you want to use it multiple times, and the value of the variable does not change, it is advisable to put it outside. If it is used only in a loop and the value is likely to change, put it inside.
Or that sentence, if you want to use it multiple times, and the value of the variable does not change, it is advisable to put it outside. If it is used only in a loop and the value is likely to change, put it inside.
Each use is assigned a new value.
Personal humble opinion: After the use of variables, remember to unset off, do not care about the outside.
Reference to the 6 floor xianglei1130 reply: Or that sentence, if you want to use multiple times, and the variable value does not change, it is recommended to put outside. If it is used only in a loop and the value is likely to change, put it inside.
Each use is assigned a new value.
Agree with upstairs, not unset also can. Since the variable value is not the same, then wherever you put it, just re-assign it to the variable before each use. No need to dwell on it or outside.
According to the requirements of the release, if placed in the loop body, the cycle period variables will not change, if placed in the loop body, the cycle of the variable will change
Defined outside the loop body, and then initialized to its original state each time it is used in the body of the loop