Download MetaTrader 5

operators: continue & break

To add comments, please log in or register
Mohammad Soubra
30247
Mohammad Soubra  

Hello

in meta trader 4:

does the operator 

continue;

 similar to the operator

 

break;
Alain Verleyen
Moderator
32842
Alain Verleyen  

Of course not.

They are used in a loop :

"continue" means stop the current iteration and continue to the next one.

"break" means, stop the loop.

Mohammad Soubra
30247
Mohammad Soubra  
Alain Verleyen:

Of course not.

They are used in a loop :

"continue" means stop the current iteration and continue to the next one.

"break" means, stop the loop.

Oh.. thanks

Mohammad Soubra
30247
Mohammad Soubra  

but in the following code, why not use the continue instead of break??

 

input int Slippage = 5;
//+------------------------------------------------------------------+
//| CLOSE ALL OPENED POSITIONS                                       |
//+------------------------------------------------------------------+
void CloseAllOpened()
{
   int total = OrdersTotal();
   for(int i=total-1; i>=0; i--)
   {
      int  ticket = OrderSelect(i,SELECT_BY_POS);
      int  type   = OrderType();
      bool result = false;

      switch(type)
      {
         //Close opened long positions
         case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,clrNONE);
         break;

         //Close opened short positions
         case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,clrNONE);
      }

      if(result==false)
      {
         Alert("Order ",OrderTicket()," failed to close. Error: ",GetLastError());
         Sleep(2000);
      }
   }
}
Stuart Browne
6246
Stuart Browne  
Mohammad Soubra:

but in the following code, why not use the continue instead of break??

 

the documentation

The operator 'break' stops the execution of the nearest external operator of 'while', 'for' or 'switch' type. The execution of the operator 'break' consists in passing the control outside the 
compound operator of 'while', 'for' or 'switch' type to the nearest following operator. The operator 'break' can be used only for interruption of the execution of the operators listed above.

 

 The "break" in your code is not in the "for" loop, it is in the "switch". It only breaks from the nearest execution loop.

"Continue" is not syntactically correct for a Switch operator, only a while or for loop

Operator 'break' - Operators - MQL4 Tutorial
Operator 'break' - Operators - MQL4 Tutorial
  • book.mql4.com
Operator 'break' - Operators - MQL4 Tutorial
Mohammad Soubra
30247
Mohammad Soubra  
ok boss
To add comments, please log in or register