Summary

Loops are a fundamental concept in programming and are used to execute tasks efficiently and repeatedly. In MQL4, there are three main types of loops: for loops, while loops, and do-while loops. Each type of loop has its own application, depending on whether you know the number of iterations or want it to depend on a condition. Additionally, infinite loops should be avoided, and there are control structures like break and continue to influence the flow of the loops.

Key Takeaways

  1. Loops enable automated repetition of tasks and significantly reduce the amount of code.
  2. In MQL4, for, while, and do-while loops are the most common loop types that can be used flexibly depending on the requirement.
  3. Loops should be carefully programmed to avoid infinite loops and can be controlled with break and continue.
Text written in chalk on a blackboard Loops in MQL4

Loops are a very important concept in programming, and they are used to repeatedly execute a set of instructions as long as a certain condition is met. In MQL4, there are different types of loops that allow you to perform tasks more efficiently and automatically.

What is a loop?

A loop is a block of code that is executed multiple times until a certain condition is no longer met. Instead of manually writing the same code repeatedly, a loop can take over that work for you.

Example

If you want to repeat a task 10 times, you can use a loop to automatically execute the code block 10 times without having to write the code 10 times.

Why are loops needed?

Loops are helpful for performing recurring tasks, such as:

The different types of loops in MQL4

In MQL4, there are mainly three types of loops:

  1. for loop
  2. while loop
  3. do-while loop

for loop

The for loop is the most commonly used loop in MQL4. It repeats instructions a fixed number of times, depending on a counter variable.

Syntax:

MQL Code

for (Initialization; Condition; Step) { // Code to be executed repeatedly }

Example

MQL Code

// This for loop prints the numbers from 0 to 9 for ( int i = 0; i < 10; i++) { Print("The number is: ", i); }

Explanation:

This loop runs 10 times and prints the values from 0 to 9.

while loop

The while loop repeats the code block as long as a condition is true. This loop is particularly useful when you don't know how many times the code should be executed, but you want it to run until a condition is no longer met.

Syntax:

MQL Code

while (Condition) { // Code that runs as long as the condition is true }

Condition: The loop runs as long as this condition is true.

Example:

MQL Code

int i = 0; while (i < 5) { Print("i is: ", i); i++; // Increment i by 1 }

Explanation:

This loop prints the values of i as long as i is less than 5.

do-while loop

The do-while loop works similarly to the while loop, but the difference is that the code will always be executed at least once, even if the condition is not met from the start.

Syntax:

MQL Code

do { // Code that runs repeatedly } while (Condition);

Condition: The loop continues until this condition is no longer met.

Example:

MQL Code

int i = 0; do { Print("i is: ", i); i++; } while (i < 3);

Explanation:

This loop runs three times, printing values of i from 0 to 2.

When to Use Which Loop?

For loop: Use it when you know exactly how many times the loop should run, for example, when you want to iterate over a fixed number of candles in a chart.

Example: You want to check the last 10 candles for specific properties:

MQL Code

for ( int i = 0; i < 10; i++) { Print("Closing price of candle ", i, ": ", Close); }

While loop: Use it when you don't know how many times the loop will run, but it needs to be executed based on a condition.

Example: You want to perform a calculation until a certain account balance is reached:

MQL Code

double balance = AccountBalance(); while (balance < 10000) { Print("Balance is still too low: ", balance); balance += 100; // Fictitious balance increase (in reality through trading) }

Do-while loop: Use it when you want to ensure that the code block runs at least once, regardless of whether the condition is met at the start.

Example: Display a message until a condition is met:

MQL Code

int attempts = 0; do { Print("Attempt No.: ", attempts); attempts++; } while (attempts < 5);

Avoid Infinite Loops!

An infinite loop is a loop that never stops because the condition always remains true. These types of loops can crash your program or heavily tax the CPU.

Example of an infinite loop (you should avoid this!):

MQL Code

int i = 0; while (i < 10) { Print("i is: ", i); // Missing i++, so the condition i < 10 always remains true }

Here, the statement to increase i is missing, so i always remains 0, and the loop runs infinitely.

Loops with break and continue

Sometimes, you may want to exit a loop early or skip the current iteration.

Break: Ends the loop immediately.

Example

MQL Code

for ( int i = 0; i < 10; i++) { if (i == 5) { break; // Ends the loop when i reaches 5 } Print(i); }

Continue: Skips the current iteration and moves to the next one.

Example:

MQL Code

for ( int i = 0; i < 10; i++) { if (i == 5) { continue; // Skips everything after continue and moves to the next iteration } Print(i); }

Questions and Answers

What is a loop in MQL4?

A loop is a code block that repeatedly executes as long as a certain condition is met. This simplifies repetitive tasks.

What types of loops are there in MQL4?

There are three main types of loops: for loops, while loops, and do-while loops. Each has a specific use case depending on the requirements for task repetition.

How does a for loop work?

A for loop is used for a defined number of iterations, where a counter variable, condition, and step are set to control the loop.

When is a while loop used?

A while loop is used when the code block should repeat as long as a condition is true, and the number of iterations is unknown beforehand.

How can loops be interrupted?

The break statement allows you to exit a loop early, while continue skips the current iteration and proceeds with the next.