Obviously, everyone writes like this:

for(int i =0; i < 5;i++){ //... } 

But I met the opinion that such a construction is absolutely wrong and is only the work of compiler developers. Accordingly, it is necessary so:

 int i=0; for(;i < 5;i++){ //... } 

What is really “right” by standard?

  • five
    Both options are correct. The only difference is the availability of the variable i after the end of the cycle. - ߊߚߤߘ
  • Obviously not. - PinkTux
  • The most correct construction is do {} while () , everything else is just syntactic sugar. - 0andriy
  • It remains to ask what is correct in the loop - i++ or ++i ... - Harry
  • @ 0andriy and Straustrup says not to use do ... while ... - mymedia

2 answers 2

The C language up to C99 does not allow variables to be declared in the for loop initialization expression. That is, in an expression before the first semicolon between the parentheses after for (...) . In addition, in the C language version below C99, single-line comments that start with two slashes (slashes) are prohibited, there are only multi-line /* ... */ .

Thus, both of your code examples are correct from the point of view of C (but starting from version C99 and higher), as well as in C ++ (any version).

Nevertheless, these pieces are a little different semantically. In the first case, after the end of the cycle, you will not be able to access its counter, the variable i , since it is declared in the scope of the cycle. In the second case, after the end of the cycle, i will be equal to 5 (if there are no transition instructions). And the variable can be used further, for example, assign something to it. However, it is not necessary to do this - it is considered a sign of bad style, and the first option is preferable.

  • 2
    Who specifically considers this style bad? In general, it all depends on the algorithm. If i used only in a loop, then it is better to declare it inside for(;;) . If, in the sense of a task (algorithm), it is needed even after a cycle (for example, it can complete by break (or etc.)), there is nothing terrible about using it (and therefore declaring before for(;;) ). - avp
  • 2
    If it is absolutely right, the use of a loop in code may well be a sign of bad code (for C ++), since so many problems are solved through the standard library of algorithms - strangeqargo

First, the choice of the first or second option, both in C ++ and in modern C, is made on the basis of whether you need to extend the scope of variable i beyond the cycle. Therefore, out of context, neither the first nor the second option in the general case can be regarded as unambiguously preferable. The question is exactly what you need in this place of the code.

Second, the for loop header allows only one declaration. This means that if your iteration process is serviced by several variables that require different decl-specifier-seq , then you want it or not, but some of them will have to be declared before the loop

 unsigned i = 0; for (double *it = container; i < n; ++i, ++it) ... 

This limitation can be circumvented in ways like

 for (struct { unsigned i; double *it; } i = { 0, container }; ii < n; ++ii, ++i.it) ... 

but usually it's not worth it.