⚡ Iteration Statement in MQL
Iteration Statement in MQL ⚡
In the programming aspect, Repetition is called Iteration. If some logic or codes requires repetitive use then, the concepts of Iterations are used.
What is Iteration?
Iteration is the looping mechanism in which you can repeat the entire block of code as many times as you required.
For Example,
You want to write "Happy Birthday" 10,000 times. then you can easily do in the world of programming.
Just use Iteration Statement and It's done.
Iteration or Repetition of code can be defined for two types of requirements:-
- Fixed Number of Times Repeat
- Repeat till certain condition "True" or "False"
Fixed Number of Repetition
For the implementation of a fixed number of repetitions, MQL provides us "For" Loop.
For Loop Iteration Statement Operator
For Loop is most useful in the MQL Forex Trading Programming. It is used to perform the repetition of a block of code any number of times. It has the following syntax.
Syntax
for( expression_1 ; expression_2 ; expression_3 )
{
// write your code here
}
How does "For Loop" works?
In the For Loop,
- expression_1 is the Initialization part or You can say the Starting Point of the Counting Variable.
- expression_2 is the condition of repetition continues. For Loop continue to execute till it reached the point where the expression_2 gives the "False" value.
- expression_3 is the increment or decrement of the variable used in expression_1.
- Body of for loop is defined by open and close braces as we see for "If-Statements".
Source Code Example - "Repeat Alert Message 10 Times"
void OnStart()
{
for(int i = 0 ; i < 100 ; i++)
Alert("Happy Birthday : ", i);
}
OutputExplanation of Output
In this example, I simply try to print Happy Birthday 100 times, starting from 0 to 99. that's why I used For Loop because the Number of repetitions is already known.
In this example, I simply try to print Happy Birthday 100 times, starting from 0 to 99. that's why I used For Loop because the Number of repetitions is already known.
You can see the "int i = 0" initialize the value of "i" with "0". "i++" increment the value of "i" with "+1" each time when loop repeats. this loop will continue to work till "i < 100 " or in other words, this condition will be true till the value of " i = 99".
Repetition till Condition True or False
Condition-based repetition can be performed using the "While" and "Do-While" loops.
While Loop Iteration Statement Operator
While Loop is used for condition-based iteration or repetition process. The Condition is checked before entering the loop body.
Syntax
while( expression_1 ) { // write your code here }
How does "While Loop" works?
In the While Loop,
- expression_1 is the essential condition for entering into a loop or iteration. If the condition of expression is found as "True" then While Loop will continue to execute till expression_1 gives "False".
- The body of for loop is defined by open and close braces.
Source Code Example - "Repeat Alert Message 10 Times"
void OnStart()
{
int i = 0 ;
while(i < 100)
{
Alert("Happy Birthday : ", i);
i++;
}
}
Output
The output will be the same as the output of For Loop Iteration. see the Image shown above.
Do-While Iteration Statement Operator
While Loop is used for condition-based iteration or repetition process. The Condition is checked after exiting the loop body. It means, In any condition, "Program will execute the code inside the do-while loop and run at least one time".
Syntax
do
{
// write you code here
}while(expression_1);
"expression_1" is the condition for repetition, "do-while" loop repeats the steps till this expression gives the result as "True".
Source Code Example
void OnStart()
{
int i = 0 ;
do
{
Alert("Happy Birthday : ", i);
i++;
}while(i > 100);
}
Output
Explanation of OutputIn this example, I have defined "i=0" and the condition is given as "i > 100" which is "False". apart from this, the do-while loop executes at least one time. If you consider another condition in which condition gives a "True" result for some step then Output Text " Happy Birthday" will be printed more than one.
Post a Comment