Control Structures in MQL4: Loops and Conditional Statements

Control structures guide the flow of a program. They determine the sequence in which statements are executed. In this lesson, we'll explore the most common control structures in MQL4: loops and conditional statements.

Conditional Statements

Conditional statements allow you to execute certain blocks of code based on whether a condition is true or false.

The if Statement

if(condition) {
    // Code to be executed if the condition is true


The if-else Statement

if(condition) {
    // Code to be executed if the condition is true
} else {
    // Code to be executed if the condition is false


The switch Statement

The switch statement in MQL4 allows for executing different code blocks based on the value of a variable. It's an efficient alternative to multiple if-else conditions.

Use switch when you need to compare a single variable against a series of distinct constant values... Use if-else when you have more complex conditions, like range checks, comparisons between different variables, logical operations (AND, OR), or when your conditions are not just about checking a variable against constant values.

int orderType = OrderType();  // Get the type of the currently selected order

switch(orderType) {
    case OP_BUY:
        Print("Handling a Buy order");
    case OP_SELL:
        Print("Handling a Sell order");
        Print("Order type is not Buy or Sell");


The for loop is a common control structure in programming used to repeat a block of code multiple times. Let's break down a simple example to understand how it works, especially for those new to programming.

Example of a for Loop

for(int i = 0; i < 10; i++) {
    total += Close[i];

Explanation of Each Part

  1. Initialization (int i = 0;): We start by creating a counter variable i and setting its initial value to 0. This counter will keep track of how many times the loop has run.
  2. Condition (i < 10;): This is a test that happens before each loop iteration. If the condition is true, the loop continues; if false, the loop stops. Here, the loop will continue as long as i is less than 10.
  3. Update (i++): After each iteration of the loop, i is increased by 1. This is written as i++, which is a shorthand way of writing i = i + 1.
  4. Loop Body: The code inside the curly braces { } is what gets executed each time the loop runs. In this case, total += Close[i] means "add the closing price at index i to the total." Close[i] refers to the closing price of the ith bar on the chart.
  5. How the Loop Works: In the first iteration, i is 0, so Close[0] (the most recent closing price) is added to total. With each iteration, i increases, adding the closing price of each successive bar to total. After 10 iterations, when i reaches 10, the condition i < 10 becomes false, and the loop stops.

This for loop pattern is widely used in programming, particularly in MQL4 for tasks like summing up values or applying calculations across a series of data points, such as financial market prices.

The while Loop

Understanding the while Loop with an Example

The while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.

Example Code:

Find the first candlestick with a closing price higher than the opening price.

    int index = 0;
    while(index < Bars && Close[index] <= Open[index]) {
    Print("First bullish candlestick found at index: ", index);


Understanding the do-while Loop with an Example

The do-while loop is similar to a while loop, but the code inside the loop body is executed at least once before the condition is tested.

Example Code:

    int counter = 0;
    do {
        Print("Counter value: ", counter);
    } while(counter < 5);


These loop structures are fundamental in MQL4 and are used widely in developing trading algorithms and strategies.


Understanding control structures is fundamental to programming in any language. They provide the logic that allows your algorithm to make decisions and repeat actions. Mastering loops and conditional statements in MQL4 will greatly enhance your ability to create powerful and efficient trading algorithms.

NEXT UP: PreDefined and Custom Functions in MQL4