We come back with you today to follow the rest of the lessons


Any program is written in the MQL4 language (indicator, expert or script) or any other programming language that executes its orders And its conditions in a descending, sequential manner (from top to bottom).

In other words, it executes the commands of the statement after another (I remind you here that the statement consists of a set of expressions that are thus composed of identifiers and operations (refer to the previous lesson)).

The statement sentence can also be defined as a line of code that tells the computer to execute something ( Show a sale signal, for example (

Example:

PHP Code:
Print ( "Hello World" );

return 0 ;


The semicolon at the End of the statement sentence is very important in the grammar of this language and forgetting it is the source of 90% of programming errors.

The process of controlling the execution of commands sequentially from top to bottom is not the only cause, but there are two other cases:

Loops and Decisions.

The programs that we write, like our human decisions, change according to the changing conditions around us. In the case of programs, the process of sequential control to execute commands can jump from one part to another part of the program.

Statements that control these jumps (move us from one command to another in the program) are called control statements.

A combination of loops and decisions

Loops
---------------------- Loops

Repeat a portion of the program for a specified number of times.

Moreover, this repetition continues as long as the conditions are right, and the loop or iteration will end with the first violation of the conditions (one of the conditions or all of them have given a false result).

When the loop (iteration) ends, you will transfer control over the execution of the commands to the next sentence to execute another command in the program or loop. . And so on.

There are two types of loops in the MQL4 language:

1- The (for)

Loop: The (for) loop is considered the easiest type of loops because all of its elements are combined in one place (as we will see), and it also executes a specific section of the program for a known and the specific number of times.

Let us take an example that shows us this:

PHP Code:
int j ;

for ( j = 0 ; j < 15 ; j ++)

Print ( j );


How does this loop work in this example?

The first line we defined a variable and called it (j) .... (Refer to the syntax grammar lesson)

The second line contains the (for) statement which consists of the reserved word (for) and parentheses () containing three expressions separated from Each other with semicolons (

PHP code:
for ( j = 0 ; j < 15 ; j ++)


These three expressions are: the starting loop expression, the variable value test expression, and the variable value increase expression.

j = 0 ....... The beginning of the loop expression
j> 15 ........ The variable value test expression
j ++ ....... The expression for increasing the value of the variable The

body of the loop is the code that will implement the above conditions For a specified number of times in this episode, and this code is:

PHP code:
Print ( j );


In our example, this specific number of loop execution is 15 times.

Note: The sentence (for) does not end with a semicolon because it is in the body of the loop, Print (j); Complete statement clause.

We will talk in some detail about these three expressions:

* The initialization expression:

This expression is executed only once with the start of the loop (the program), and it gives a default starting value (which is zero in our example here j = 0).

We can set the loop variable outside (Before) the same loop as in our example:

PHP code:
int j ;


Alternatively, we can also set the variable inside the loop itself, as in this example:

PHP code:
for ( int j = 0 ; j < 15 ; j ++)


The previous two lines of codes are equal, but the purpose of each of them may differ somewhat (we will know the details of the variables and their objectives in the study of the variables, God willing).

Only in the case of the external numbers of the variable makes all the lines of the loop deal with it, but in the case of the internal numbers, it makes a sentence line (for Only you deal with.

We can also make two initial expressions for the loop (with two variables) and separate them with a comma (,) only.

Example:

PHP code:
int I;

int j ;

for ( i = 0 , j = 0 , i < 15 ; i ++)

Print ( i );


* The Test expression: The

Test expression always uses comparison expressions using comparison operations (please refer to the comparison operations in the previous lesson).

This expression (in our example here (j> 15) makes a continuous comparison between the value of j and the number 15 throughout the loop run. Its function is to determine the result of the comparison, either real (meaning that the value of j is less than 15), so the body of the loop continues and executes the (print j) command, or false (meaning that the value of j is not less than 15) and here the loop stops working, and the program moves to The following syntax. (I will explain the life cycle of the program in a later lesson, God willing, in better detail, only here please pay attention to the function of each part and review it)

* The increment expression:

The increment expression changes the value of the loop variable (in our example here j by ++ j) by increasing its value of 1.

This expression is implemented as the last of the three steps in the cycle (starting the episode, testing it, and executing the body of the episode).

Just as we can use more than one expression for the beginning of the loop (and more than one variable), we can also use more than one expression for addition (or decrease) and separating them with a comma (,).

Example:

PHP code:
int I;

int j ;

for ( i = 0 , j = 0 ; i < 15 , i <; i ++, j ++)

Print ( i );


However, we cannot use more than one test expression only in a sentence.

Another note for the expression increase, which is that we can use the expression decrease also, as in this example:

PHP code:
int I;

for ( i = 15 , i > 0 , i <, i -)

Print ( i );


In this example, the variable I will start by taking the default value 15 and then start the loop, and in the implementation stages every time it decreases the value by one and then performs a test I> 0 ... and so on.

In the End, the program (the loop) will produce these results for us: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1.

** Loop body:

We can use multiple statements in the ring body to produce the loop results.

In our previous example, we used only one sentence (print j) in the body of the loop, and we can add more than one sentence to it.

When adding more than one sentence in the body of the loop, we define them with these brackets {}.

Example:

PHP code:
for ( int i = 1 , i <= 15 ; i ++)

{

Print ( i );

PlaySound ( "alert.wav" );

}


In this example, the body of the loop contains two syntaxes; the program will execute the first and then execute the second each time.

Moreover, do not forget to put a semicolon ( at the End of every sentence.

** The break statement: it

Is a reserved word from MQL4 language words whose function is to end the loop at a specified value.

It is placed as an execution sentence in the body of the loop to terminate the loop when the variable reaches a specified value and transfer the program to another episode or another part.

Illustrative example:

PHP code:
for ( int i = 0 ; i < 15 ; i ++)

{

if (( i == 10 )

break;

Print ( i );

}


In this example, the loop will run until the variable I reaches the value 10, after which the break terminates the loop.

In the End, the program will produce these results: 0 1 2 3 4 5 6 7 8 9.

** The Continue Statement: The

Break statement took us out of the episode, but this continuation sentence will bring us back to the episode in case we exit it.

Illustrative example:

PHP code:
for ( int i = 0 ; i < 15 ; i ++)

{

if ( i == 10 ) continue;

Print ( i )

)


In this example, the loop will be executed until the value of the variable I reaches 10, and then it finds the reserved word (continue) to bring us back to the loop without taking the result (10) out of the loop.

The results are thus out of the program: 0 1 2 3 4 5 6 7 9 11 12 13 14 (note that there is no result 10).

2- The (while)

Loop: The (for) loop we usually use in cases of repetition of times you know the number and program on its basis, well what will happen if you do not know the number of iterations you want to use (the repetition may continue forever).

That is why the (while) episode appeared.

The while loop is similar to the for loop in the test expression only, but it does not have the starting and the addition expression.

Let us see an illustrative example:

PHP Code:
int i = 0 ;

while ( i < 15 )

{

Print ( i );

i ++;

}


In this example, you will notice the following:

a- The loop variable was set up and started before entering the loop. You cannot set or start the variable inside the loop with parentheses like the loop (for).

b- The (++ i) is not a different expression as you think, but the body of the loop must contain any sentence that enables it to change the value of the variable. Otherwise, it will never end (for example, if ++ i is not present, the program will launch the result zero to what is not End)

* How does the previous example work?

The while clause contains only the test expression only, and you will still test the condition every time by entering the loop. If the test result (comparison) is correct, the loop will continue, and if it is false, the loop will end and move to another sentence in the program .. and so on.

In our example, the loop will continue to execute its commands until it reaches 16, in which case I <15 is false, and the loop ends.

As I told you that the two rings are similar, there are also other similarities, such as:

1- We can use the fraction sentence and the continuation clause in both.

2- You can put one or more sentences in the body of the two rings, and in the case of placing more than one, they must be placed inside the brackets {}.


In the next lesson, God willing, we will talk about the episodes and decisions (Part2