MQL4 while Loop Operator

Overview

The while operator executes a specified operator repeatedly as long as a checked expression remains true. The syntax is while(expression) operator;.

Behavior

  • The expression is evaluated before each execution of the operator.
  • If the expression evaluates to true, the operator is executed.
  • This process continues until the expression evaluates to false.
  • If the expression is false from the beginning, the operator will not be executed at all.
  • Constraints and Recommendations

  • For loops expected to handle a large number of iterations, it is advisable to check for forced program termination using the IsStopped() function within the loop condition to prevent infinite loops or unresponsive scripts.
  • Syntax

    while(expression)
      operator;
    

    Example

    while(k < n && !IsStopped()) {
      y = y * x;
      k++;
    }
    

    Related Topics

  • [Initialization of Variables](/basis/variables/initialization)
  • [Visibility Scope and Lifetime of Variables](/basis/variables/variable_scope)
  • [Creating and Deleting Objects](/basis/variables/object_live)
  • [Loop Operator for](/basis/operators/for)
  • [Switch Operator](/basis/operators/switch)