I have been wondering and couldn’t find a clear answer on this subject.
Imagine the following two examples.
int i;
for(i=0;i<maxLimit;i++)
{
//code here
}
2nd example
for(int i=0;i<maxLimit;i++)
{
//code here
}
Basically if they are like that I believe both would behaviour exactly the same but my questioning comes into place when considering this
while(!file.EOF)
{
for(int i=0;i<maxLimit;i++)
{
//code here
}
}
When the 2nd example is inside a loop itself how does it perform? Is it more efficient to declare it beforehand and simply put it back to 0 or is the execution time the same as declaring it inside?
Personnally I usually put it like number 2 because I find it better visually speaking. But I,m wondering if it’s inside a loop what the effect of it would be.
Thank you for your answers.
Most modern compilers are sufficiently sophisticated that any decision you make at this level is likely to make no difference whatsoever (to clarify, after analysis the compiler may have transformed both into the same internal representation – being the most efficient implementation). But if you’re not convinced, the golden rule in this kind of case is to check with a profiler.