Loop question

 

Hello.

I found this script online and I have a question.


for(int j = 2; j <= 10; j = j + 2) 
      {
      Print("The value of j is " + (string) j + ".");
      }

It is supposed to print 2,4,6,8,10.


However I am confused because j+2 is included within the loop logic.

So the way it looks to me is ( j = 2 (and if) j <= 10 (then) add 2 to j (then) print.


But this means on the first step it would add 2 to j and it would skip 2 and print 4,6,8,10.


So my question is, why is j+j=2 skipped on the first loop run?


 
Colin Leamy:

Hello.

I found this script online and I have a question.


for(int j = 2; j <= 10; j = j + 2) 

      {

      Print("The value of j is " + (string) j + ".");

      }

It is supposed to print 2,4,6,8,10.


However I am confused because j+2 is included within the loop logic.

So the way it looks to me is ( j = 2 (and if) j <= 10 (then) add 2 to j (then) print.


But this means on the first step it would add 2 to j and it would skip 2 and print 4,6,8,10.


So my question is, why is j+j=2 skipped on the first loop run?


The first statement says that the loop starts with j=2.

The second statement says that the loop will be executed until j<=10 or in other words until it becomes equal to ten.

The third statement j+2 says that after every loop  j will be increased by two. And I think it is written somewhere in the documentary that the for loop index variable is increased each time only AFTER the loop body was executed.

So the first time around j will be 2 and the value of j will be printed out, then increased to 2+2=4.
In the second loop j=4 is printed out, 
then increased: 4+2=6 (because the variable j was no longer two already, but it had been set to four)
6 is printed in the third loop, then will be increased again 6+2=8 and then to ten. Now the print function is executed one last time because it is j<=10.
If you had set the loop head j<10 it would not run this last time.

You see how the variable is reused at every run to store the next higher number.
 

What is preventing j=2 from resetting j to 2 on every loop?

I don't see any explicit ordering or keyword that prevents J from equaling 2 at the beginning of every loop.

I am having a difficult time understanding how the computer sees this logic.

 

Because this ...

for( int j = 2; j <= 10; j = j + 2 ) 
{
   Print( "The value of j is " + (string) j + "." );
}

... is short hand for this ...

int j = 2;
while( j <= 10 ) 
{
   Print( "The value of j is " + (string) j + "." );
   j = j + 2;
}


 
Fernando Carreiro:

Because this ...

... is short hand for this ...


Oh so the structure of for loops are always the same?

for(variable = x; x (operator) y; x (operator) z?

 
Colin Leamy:

What is preventing j=2 from resetting j to 2 on every loop?

I don't see any explicit ordering or keyword that prevents J from equaling 2 at the beginning of every loop.

I am having a difficult time understanding how the computer sees this logic.

j is not meant to be a constant, but a variable. It adds up and drives the loop forward. 

You could build a TickCount, just declare a global variable 

int TickCount=0;

And write 

TickCount++;    and
Print("TickCount: ", TickCount);

in the OnTick() function and you can watch it run up in the Strategy Tester. But why doesn't it go back to zero?
 
Colin Leamy: Oh so the structure of for loops are always the same? for(variable = x; x (operator) y; x (operator) z?

Maybe this will help clear it up. This ...

for(expression1; expression2; expression3)
   operator;

... is equivalent to this ...

expression1;
while(expression2)
{
   operator;
   expression3;
}
Documentation on MQL5: Language Basics / Operators / Loop Operator for
Documentation on MQL5: Language Basics / Operators / Loop Operator for
  • www.mql5.com
Loop Operator for - Operators - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
pennyhunter:
j is not meant to be a constant, but a variable. It adds up and drives the loop forward. 

You could build a TickCount, just declare a global variable TickCount=0;
And write TickCount++; in the OnTick() function.
You can watch it run up in the Strategy Tester. But why doesn't it go back to zero?

Yeah but the part that is confusing me is that the beginning of the loop says  j = 2.

So even if was j = 1000 before the loop began, it would be forced to become 2 as soon as it hits the j = 2 function.

 
Fernando Carreiro:

Maybe this will help clear it up. This ...

... is equivalent to this ...

Okay so this is just the intrinsic structure of for()?

Okay got it. :)

 
Colin Leamy:

So even was j = 1000 before the loop began, it would be forced to become 2 as soon as it hits the j = 2 function.

Nono the j=2 expression is only executed in the beginning. It won't go back down except if you put a j=j-2 expression in the body which is the part below the head that is ex ecuted on every run.
 
pennyhunter:
Nono the j=2 expression is only executed in the beginning. It won't go back down except if you put a j=j-2 expression in the body which is the part below the head that is ex ecuted on every run.

Okay so the first expression initializes, the second expression cancels the loop, and the third expression runs after body but before the next loop correct?