+ 2

For loop

is declaring a variable inside for loop good?

1st Jan 2018, 4:55 AM
sal sal
sal sal - avatar
2 Answers
+ 10
There are better ways to avoid repeated initializations, by placing the declaration outside the for loop and initialising it in the loop if required, if this does not make a difference to what you originally intended to do. Performance-wise, the difference should usually be too subtle to notice, seeing that your compiler might optimize it for you anyways.
1st Jan 2018, 5:12 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
no
1st Jan 2018, 10:58 AM
Ajith Bhat
Ajith Bhat - avatar