MQL4 Reference

MQL4 Basics:Operators

Language operators describe some algorithmic operations that must be executed to accomplish a task. The program body is a sequence of such operators. Operators following one by one are separated with a semicolon.

One operator can occupy one or several lines. Two or more operators can be located in the same line. Operators that control over the execution order (if, if-else, switch, while and for) can be nested into each other.


if(Month() == 12)
  if(Day() == 31) Print("Happy New Year!");

Compound operator

A compound operator (a block) consists of one or more operators of any type enclosed in braces {}. The closing brace must not be followed by a semicolon (;).


   Print("invalid position x=",x);

Expression operator

Any expression followed by a semicolon (;) is an operator. Here are some examples of expression operators:

Assignment operator:

y=x=3; // error

Assignment operator can be used in an expression only once.

Function call operator:

Function_name(argument1,..., argumentN);

Empty operator

It consists of a semicolon (;) only and used to denote a null body of a control operator.

Break operator

A break operator terminates the execution of the nearest nested outward switch, while, or for operator. The control is given to the operator that follows the terminated one. One of the purposes of this operator is to finish the looping execution when a certain value is assigned to a variable.


// searching for the first zero element

Continue operator

A continue operator gives control to the beginning of the nearest outward cycle while or for operator, the next iteration being called. The purpose of this operator is opposite to that of break operator.


// summary of nonzero elements of array
int func(int array[])
   int array_size=ArraySize(array);
   int sum=0;
   for(int i=0;i<array_size; i++)
      if(a[i]==0) continue;

Return operator

A return operator terminates the current function execution and returns the control to the calling program. A return(expression); operator terminates the current function execution with result transmission. The operator expression must be enclosed in parentheses and should not contain an assignment operator.


int CalcSum(int x, int y)

In functions with the value of void type to be returned, the return operator must be used without the expression:

void SomeFunction()
   return;    // this operator can be deleted

The right brace of the function means implicit execution of the return operator without expression.

Conditional operator if-else

If the expression is true, operator1 is executed and the control is given to the operator that follows operator2 (operator2 is not executed). If the expression is false, operator2 is executed.

if (expression)

The else part of the if operator can be omitted. Thus, a divergence may appear in nested if operators with omitted else part. In this case, else addresses to the nearest previous if operator in the same block that has no else part.


//   The else part refers to the second if operator:
  if(y==2) z=5;
  else     z=6;

//   The else part refers to the first if operator:
   if(y==2) z=5;
else        z=6;

//   Nested operators
else if(x=='b')
else if(x=='c')
   y = 4;
else Print("ERROR");

Switch operator

It compares the expression value with constants in all variants of case and gives control to the operator that corresponds with the expression value. Each variant of the case can be marked with an integer or literal constant or with a constant expression. The constant expression cannot contain variables or function calls. Expression of the switch operator must be of integer type.

   case constant: operators
   case constant: operators
   default: operators

Operators connected with the default label are executed if none of the constants in case operators equals the expression value. The default variant must be necessarily final. If none of the constants corresponds to the expression value and the default variant is not available, no actions are executed. The keyword case and the constant are just labels, and if operators are executed for some case variant, the program will further execute the operators of all following variants until break operator occurs. It makes it possible to bind a subsequence of operators with several variants.

A constant expression is calculated during compilation. No two constants in one switch operator can have the same values.


   case 'A':
      Print("CASE A");
   case 'B':
   case 'C':
      Print("CASE B or C");
      Print("NOT A, B or C");

Cycle operator while

If the expression is true, the operator is executed until the expression becomes false. If the expression is false, the control will be given to the next operator.


An expression value has been defined before the operator is executed. Therefore, if the expression is false from the very beginning, the operator will not be executed at all.



Cycle operator for

Expression1 describes the cycle initialization. Expression2 is the conditional test for the cycle termination. If it is true, the loop body for operator will be executed. The cycle repeats until Expression2 becomes false. If it is false, the cycle will be terminated, and the control will be given to the next operator. Expression3 is calculated after each iteration.

for (Expression1; Expression2; Expression3)

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


Any of the three or all three expressions can be absent in the for operator, but the semicolons (;) that separate them must not be omitted. If Expression2 is omitted, it is considered constantly true. The for (;;) operator is a continuous cycle equivalent to the while(1) operator. Either expression 1 or 3 can consist of several expressions combined by a comma operator ‘,’.


for(x=1;x<=7;x++) Print(MathPower(x,2));
for(;;) { Print(MathPower(x,2)); x++; if(x>10) break; }
for(i=0,j=n-l;i<n;i++,j--) a[i]=a[j];
This article url:

Please login to post comment, if you haven't account you can register free