Cycle Operator 'for' questions

 

Hello MQL4 community,


First reference: https://book.mql4.com/operators/for

According to the first reference, a format example of the 'for' operator is written as such:

  for (Expression_1; Condition; Expression_2)         // Cycle operator header
      One operator, cycle body                     // Cycle body is one operator


Second reference: https://docs.mql4.com/basis/operators/for

According to the second reference, the example provided of the 'for' operator is described as such:

for (Expression1; Expression2; Expression3)
   operator;


Why does this first example contain a "condition" as a second parameter while the second example contains "Expression2" as a second parameter?

What is the meaning of this contradiction?

Does it even matter?

If I am to use 'for' cycle operator, I believe I must have keen understanding of all its parameters.

Thank you.

 

Perhaps you might better understand a for loop by first understanding a while loop . . .   a for loop is just a while loop with initial condition and increment built into it . . .

Check out the documentation on  for it says . . . 

"The for operator is equivalent to the following succession of operators:"

Expression1;
while(Expression2)
  {
   operator;
   Expression3;
  };

 
Specifically related to your question,  "Why does this first example contain a "condition" as a second parameter while the second example contains "Expression2" as a second parameter?"  an expression can be a condition,  a condition results in a true or false value,  for example  (a > b)  or (c || d)  or  (OrdersTotal() >= 0) ,  also the documentation also says . . .  "Expression2 is the conditional test for the cycle termination."

 

Simon,

an expression can be a condition

So, parameter two in a 'for' operator header can be an expression OR condition? I assume Expression_1 and Expression_3 must remain expressions and not change to conditions correct? I don't see why expression 1 & 3 could not also be conditions. Both expressions 1&3 are just parameters to be filled. (being a condition COULD alternatively be parameter two aka Expression_2's purpose)

Please confirm thoughts.

Expressions can be arithmetical operations (2+2=4), assignment, etc. (all listed in MetaEditor Dictionary).

Condition is true || false (examples: a>b, a>=b, a==b, etc.).

Operations are contained within the MetaEditor dictionary as well (examples: Break, Continue, Return, while, for, etc.).

Thank you

 
WhooDoo22:

Simon,

an expression can be a condition

So, parameter two in a 'for' operator header can be an expression OR condition? I assume Expression_1 and Expression_3 must remain expressions and not change to conditions correct? I don't see why expression 1 & 3 could not also be conditions. Both expressions 1&3 are just parameters to be filled. (being a condition COULD alternatively be parameter two aka Expression_2's purpose)

Learn how to use while correctly . . . .  a for loop is a while loop just arranged in a more aesthetically pleasing way.   Then the expressions vs conditions thing will all make sense . . .  do yourself a favour and reduce the options for head aches . . . .  study  while  

Documentation - while   

Book - while
 
WhooDoo22: So, parameter two in a 'for' operator header can be an expression OR condition?
Any expression results in a number. The number, converted to a boolean is a condition. Same thing. You say tuh-mey-toh, and I say tuh-mah-toh
 

Simon, I'll practice use of while operation in code. do yourself a favour... I like the sound of this! :) Thank you.

William, The number, converted to a boolean is a condition... (1=true,0=false). Much thanks.

Thank you.

 

Simon,

I've written an EA which should ;) be able to do as you requested.

I am sharing a combination of this EA coupled with a snap shot of the result from the "Experts" tab in the Terminal.


The EA seems to be able to select the USDJPY order and print its information to the Experts tab. Why does the EA not close the order as well as print the order's info?

Thank you.

 
WhooDoo22:

Simon,

I've written an EA which should ;) be able to do as you requested.

I am sharing a combination of this EA coupled with a snap shot of the result from the "Experts" tab in the Terminal.


The EA seems to be able to select the USDJPY order and print its information to the Experts tab. Why does the EA not close the order as well as print the order's info?

Thank you.

You said you understood braces ?  https://www.mql5.com/en/forum/142732


Please explain your code and what it is meant to do . . .  in particular talk me through the while please.
 

Simon,

Please explain your code and what it is meant to do . . .  in particular talk me through the while please.

I will do my best to explain code and it's purpose.


Code Purpose:

1. Select USDJPY order from pool of three orders.

2. Modify TP and SL of USDJPY order.

3. Execute OrderPrint() function.

4. Close USDJPY order.


Code Explanation:

Firstly, variables are declared.

   int i;
   double bid_ask;

Secondly, 'while' loop is executed.

'while' loop logic:

OrderSelect() function uses variable 'i' as a substitute for a number 1,2,3,etc.. (just keep this in mind please)

So, if the variable i within the OrderSelect() function and OrderSymbol() function is NOT USDJPY, increase variable i by 1. ( i++; )

   while(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()!="USDJPY")

         i++;

The 'while' loop increases 'i' variable until OrderSelect() function selects the proper order ticket AND the OrderSymbol() function selects a USDJPY order. When both objectives have been accomplished, the while loop becomes false and control is passed below i++; (the while loop body).

Thirdly, OrderModify() function is executed.

OrderModify(i,OrderOpenPrice(),50*Point,50*Point,0,CLR_NONE);

Fourthly, OrderPrint() function is executed.

OrderPrint();

Fifthly, parameters are included to discern if the order to be closed is a buy or sell order.

         if(OrderType()==OP_BUY){bid_ask=Bid;}
         if(OrderType()==OP_SELL){bid_ask=Ask;}

Sixthly, once the program distinguishes if the order is buy or sell, it executes the OrderClose() function.

OrderClose(i,OrderLots(),bid_ask,3,CLR_NONE);

Seventhly (and lastly), the program includes a comment which will show variable 'i' to confirm the ticket number is indeed the ticket on USDJPY during visual testing in strategy tester.

Comment("i = " ,i);

Thank you.

 
WhooDoo22:

Simon,


'while' loop logic:

OrderSelect() function uses variable 'i' as a substitute for a number 1,2,3,etc.. (just keep this in mind please)

So, if the variable i within the OrderSelect() function and OrderSymbol() function is NOT USDJPY, increase variable i by 1. ( i++; )

The 'while' loop increases 'i' variable until OrderSelect() function selects the proper order ticket AND the OrderSymbol() function selects a USDJPY order. When both objectives have been accomplished, the while loop becomes false and control is passed below i++; (the while loop body).

Sorry,  I was misreading your code . . .  thanks for the explanation,  I understand what you are doing now.  The problem you have is very simple . . .  just look at the error message in the screen grab you posted . . .  look at wht OrderPrint() produces,  including the order number,  then look at the error.

If you had included checking of return values and error reporting you probably would have seen your problem yourself.
 

Simon,

I do not see what it is you are seeing but ;) I will promptly include the GetLastOrder() function to shed some illumination on this issue. Apologies for posting a question without first including the GetLastOrder() function with the code.

Thank you.

Reason: