Is there any significant difference between following code:
for(int i=0;i<100;i++){
int s =i;
}
and
int s=0;
for(int i=0;i<100;i++){
s =i;
}
Is there any significant difference between following code:
Just the difference of scope of int s. In former, it would not be visible outside the for loop. Whereas, it would be in the later case. As far as best practices is concerned, you should try to minimize the scope of local variables as far as possible. So, if you are not using int s outside the for loop, then it's better to declare it inside only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With