Variable `i` already defined?

 
How come is i already defined since i is part of for scope, does anyone know about this?

I know the fix, but this has never happened to me once, and from some time ago, it just doesn't stop..

Thanks! 

void OnTick()
{
   for (int i = 10 - 1; i >= 0; i--) {
      Print(i);
   }

   for (int i = OrdersTotal() - 1; i >= 0; i--) {
      Print(i);
   }
}
 

In newer versions of MQL4 (and some C++ compilers), the scope of a variable declared in a for loop (like int i) extends to the entire function, not just inside the loop.

This means: the first declaration of int i makes i exist for the whole OnTick() function, so when you try to declare it again in the second loop, the compiler complains.

The problem didn't exit previously as newer versions of MetaEditor (or MQL4/5 compilers) became stricter and enforce this rule.

 
James McKnight #: In newer versions of MQL4 (and some C++ compilers), the scope of a variable declared in a for loop (like int i) extends to the entire function, not just inside the loop.

Incorrect. It has always extended in MQL4, unless you used strict. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

 
Daniel Alarcon:
How come is i already defined since i is part of for scope, does anyone know about this?

I know the fix, but this has never happened to me once, and from some time ago, it just doesn't stop..

Thanks! 

define i outside the scope so it won't change

 
Daniel Alarcon:
How come is i already defined since i is part of for scope, does anyone know about this?

I know the fix, but this has never happened to me once, and from some time ago, it just doesn't stop..

Thanks! 

Change the second “int i” to just “i” .. the error gives you this info. 
 
William Roeder #:
in MQL4, unless you used stri

Yes, I think your conclusion is correct.

 
Thanks everyone for the support! I appreciate it! 😁😁
 
William Roeder #:

Incorrect. It has always extended in MQL4, unless you used strict. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

Oh this must have been the thing I changed, since I didn't know what was this for! Interesting.. Thank you so much 🙌