MQL5 TUTORIAL BASICS – 9 HOW USE THE FOR LOOP TO COUNT

In this video we are going to find out how to increase or decrease a “counter” by using the “for” loop, this one is counting until it reaches the value of 10,000, so let’s find out how to do that.
To get started please click on the little icon here or press F4 in Metatrader, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleForLoop”, click on “Continue”, “Continue” and “Finish”.
Now you can delete everything above the “OnTick” function, let’s also remove the two comment lines.
We start by creating a “counter” variable, this one is an integer (int) variable called: “counter” and the initialization value will be 1, we also want to define an end value (endvalue), in our case it’s 10,000 and inside of the “OnTick” function we want to use the “for” loop, now when I mark “for” and press F1 I will see that the “for” loop operator will need 3 expressions and if you have never seen a “for” loop before this might look a little weird but actually it’s not so hard to understand.
The first expression here is the starting value, in our case it’s the “counter” value 1. The second expression checks a condition – in our case we want to check if the value of the “counter” is below the “endvalue” – and in the third expression we are doing something with the “counter”, in our case we are increasing it by 1 that’s what “counter++” means, we also could say “counter” equals “counter” plus 1 and whatever is inside this curly braces here will be executed as long as this condition here is true, in our case we just print out the text: “the counter equals” followed by the current value of the “counter”.
So far so good, please click on the “Compile” button here and if you don’t see any errors down here you can click on the little symbol here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester” or we press CTRL and R, let’s pick the new file: “SimpleForLoop.ex5”, please enable the visualization here and start a test.
And inside of the “Journal” tab you should see the “counter” is actually working, it increases the value until it has reached 9,999, so far so good but it’s also possible to create a countdown.
So let’s exchange the values here, now the “counter” has a starting value of 10,000 and the “endvalue” should be 1, we start with the “counter” value and as long as the “counter” is bigger than the “endvalue” we want to count down and subtract 1 from the current “counter” value.
Let’s compile that one, restart the test and this time we have a countdown.
Well, this one ends with the last entry: “Counter equals 2” and that’s because in our second expression we have checked if the “counter” is bigger than the “endvalue”.
We could also say bigger or equal here, so let’s recompile and this time the last value should be 1 and that’s actually the case. You could also use the “for” loop to increase or decrease the “counter” with other values, so let’s pick 10. One last compilation and this time you see that our countdown is working and we have a step size of 10 and in this little video you have learned how to use the “for” loop to count and increase or decrease “counter” values and you have coded it yourself with a few lines of MQL5 code.