Very simple question, but I can’t find an answer anywhere. Common coding practice has a for loop with i as an int, as in:
for (int i = 0; i < something; i++)
If you have nested for loops, you obviously need your inner loop(s) to have different variable names, to differentiate between them.
However, if you have multiple for loops in one method, that aren’t nested, is it bad practice to declare each of them as int i? I know that they have no scope outside of that particular declaration, so I know it won’t cause any errors, but is it good practice?
*Edit:
Hi all. Thanks for your fantastic (and extremely prompt!) answers. I wish I could have chosen all as answers. I didn’t think it was bad practice, just wanted to be sure. Thanks for all your help.*
It isn’t considered a bad practice,you can name the variables same (there’d be no compiler errors), because, those variables are only confined within that block .in fact, widely used loop variables are named like
i,j,k,a,b