$ £ ¥
¥ £ $

Operators in MQL4

What is a program? A program is a sequence of operations put together to perform one or more tasks. These operations are performed through operators. In this guide, you can see what operators are in MQL4 programming language and, more importantly, how to use them.

An operator can be defined as a character, or sequence of characters, that drives MetaTrader to perform an action. A very simple example that is used with variables is the = character. When you write int i = 10;, the operator = is an assignment operator, while ; (a semicolon) is an empty operator to finish the action.

MQL4 has many types of operators, too many to cover them all in one guide. You will only learn about a few of them for the moment. To start getting familiar with some code, you will see examples and explanations for them with comments.


Compound Operator

void OnStart()
{
   /*
   Compound operator is a group of operators contained between curly brackets { }
   */
   if (1 == 1)
   {
      Print("Hello");
      Print("World");
   }  
}

Terminator Operator

void OnStart()
{
   // The semicolon ; terminates the line.
   // The equal sign = assigns a value.
   int a = 10;
   string s = "Hello";
}

Conditional Operators

void OnStart()
{
   // Conditional operators execute an action depending on whether a condition is verified.
   // These can be categorized as conditional: if-else, switch-case, and tertiary operator ?

   int i = 10;

   // "if" executes an action if the condition is verified.
   if (i > 0)
   {
      Print("i is big"); // Program prints "i is big" if i is greater than 0.
   }
   else Print("i is small"); // Program prints "i is small" if i is 0 or a negative number.

   // "switch-case" checks a variable/expression over a value and performs the associated action.
   switch(i)
   {
      case 1:
      Print("i is 1"); // Program prints "i is 1" if i has value 1, then it exits the switch case.
      break;
      case 10:
      Print("i is 10"); // Program prints "i is 10" if i has value 10, then it exits the switch case.
      break;
      default:
      Print("i is not 1 or 10"); // Program prints "i is not 1 or 10" if none of the conditions is verified.
      break; // Then it exits the switch case.
   }
   // The tertiary operator may be tricky: it checks a condition and if true, it assigns the first value;
   // if false, it assigns the second value.
   int j = (i == 10) ? 1 : 0; // If i is equal to 10 j is assigned the value 1, otherwise it is assigned 0.
}

Loop Operators

void OnStart()
{
   // Loop operators perform the same action a number of times, that is why they are called "loop operators".
   // These operators are: "for", "while", and "do while". And they have some differences.
   
   // The loop "for" defines a variable i = 1 and prints it for as long as i <= 10,
   // i is incremented with i++ at each execution.
   // This loop will print the sequence of numbers from 1 to 10:
   for (int i = 1; i <= 10; i++)
   {
      Print(i);
   }
   
   i = 1;
   // "while" is similar to "for", but the variable is defined beforehand,
   // and the body of the loop must include the update of the variable.
   // This loop will also print the sequence of numbers from 1 to 10.
   while (i <= 10)
   {
      Print(i);
      i++;
   }
   
   i = 1;
   // "do while" is similar to the previous two,
   // but the big difference is that the condition is checked after the execution.
   // This means that "do while" is always executed at least once.
   // This "do while" will print the sequence of numbers from 1 to 10 too.
   do
   {
      Print(i);
      i++;
   }
   while(i <= 10);
}

This was just an introduction to operators in MQL4. There is a wide variety of them for different purposes. The more you will code, the more operators you will face, learning to embrace them and use them in order to achieve your coding goals.

If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.