Posted By:
30 Jul, 2014 10:01 pm PDT

While loop in MetaTrader

WHILE LOOP

The most powerful functionality of MQL4 is the possibility to organize cycles (loops).

When creating application programs, you may often use the repeated calculations, which are mostly the repeated program lines. In order to make the programming comfortable and the program itself user-friendly, we use cycle operators.

The full-format cycle operator 'while' consists of the header containing a condition, and the executable cycle body enclosed in braces.

while ( Condition )// Header of the cycle operator
{// Opening brace
Block of operators// The body of a cycle operator may consist..
that compose the cycle body//.. of several operators
}// Closing brace

Attachment:None


Example of while loop:

//+------------------------------------------------------------------+
//| whileloop.mq4 |
//| Copyright 2014, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int price=10;
int OnInit()
{
//---
while(price<=50){
Alert("The value of price is "+price);
price+=10;
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---

}
//+------------------------------------------------------------------+
 
Was it useful?  




Please login to reply to this problem.