Seems buggy, yes...
If explicitly declared, it works.
//+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ int start() { for(int i = 0; i < 5; i++){ bool Bool; int Int; Print("Bool = ", Bool, " Int = ", Int); Bool = true; Int = i; } // EXPLICIT for( i = 0; i < 5; i++){ bool Bool1 = false; int Int1 = 0; Print("Bool1 = ", Bool1, " Int1 = ", Int1); Bool1 = true; Int1 = i; } return(0); }
Output
2009.05.24 02:26:47 test AUDCHF,H1: uninit reason 0 2009.05.24 02:26:47 test AUDCHF,H1: Bool1 = 0 Int1 = 0 2009.05.24 02:26:47 test AUDCHF,H1: Bool1 = 0 Int1 = 0 2009.05.24 02:26:47 test AUDCHF,H1: Bool1 = 0 Int1 = 0 2009.05.24 02:26:47 test AUDCHF,H1: Bool1 = 0 Int1 = 0 2009.05.24 02:26:47 test AUDCHF,H1: Bool1 = 0 Int1 = 0 2009.05.24 02:26:47 test AUDCHF,H1: Bool = 1 Int = 3 2009.05.24 02:26:47 test AUDCHF,H1: Bool = 1 Int = 2 2009.05.24 02:26:47 test AUDCHF,H1: Bool = 1 Int = 1 2009.05.24 02:26:47 test AUDCHF,H1: Bool = 1 Int = 0 2009.05.24 02:26:47 test AUDCHF,H1: Bool = 0 Int = 0 2009.05.24 02:26:47 test AUDCHF,H1: loaded successfully 2009.05.24 02:26:38 Compiling 'test'
Right, thats buggy.
It seems like a 2 pass compiler. In pass 1 it sees all declarations in a function, sticks them in memory. If it finds something declared in the memory, it will not redeclare again. So next time it sees a declaration within the loop, it will not redeclare, but use from memory.
There are some other oddities as well. Arrays are automatically static (sticky). Try it.
JForex.

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi,
I would expect both these variables to initialize to their default values (false, 0) in each for loop iteration, because I declare them inside the loop.
This is how other languages (like Java) would handle this.
But interestingly, both these variables preserve their changed values in each iteration, despite redeclaration in each iteration.
Can someone tell me why is this, or how does MQL handle this code?
Thanks,
JForex.