MQL4参考 语言基础 操作符 循环操作符 do while

Loop Operator do while

The for and while loops check the termination at the beginning, not at the end of a loop. The third loop operator do - while checks the condition of termination at the end, after each loop iteration. The loop body is always executed at least once.


First the operator is executed, then the expression is calculated. If it is true, then the operator is executed again, and so on. If the expression becomes false, the loop terminates.


If it is expected that a large number of iterations will be handled in a loop, it is advisable that you check the fact of forced program termination using the IsStopped() function.


//--- Calculate the Fibonacci series
   int counterFibonacci=15;
   int i=0,first=0,second=1;
   int currentFibonacciNumber;
      Print("i = ",i,"  currentFibonacciNumber = ",currentFibonacciNumber);
      i++; // without this operator an infinite loop will appear!
   while(i<counterFibonacci && !IsStopped());

See also

Initialization of Variables, Visibility Scope and Lifetime of Variables, Creating and Deleting Objects