• No results found

Examples of the Operator 'switch'

In document MQL4 Book - PDF format (Page 94-98)

Below is the problem solution using the operator 'switch' (Expert Advisor pricealert.mq4):

//--- // pricealert.mq4

// The code should be used for educational purpose only.

//---

The program must pass the control to the first operator that follows the ":" (colon) of the variation 'case', the Constant value of which is the same as the value of the Expression, and then execute one by one all operators composing the body of the operator 'switch'. The condition of equality between the Expression and the Constant is tested in the direction from top to bottom and from left to right. The values of Constants in different variations 'case' must not be the same. The operator 'break' stops the execution of the operator 'switch' and passes the control to the operator that follows the operator 'switch'.

Problem 18. Compose a program where the following conditions are realized: If the price exceeds the predefined level, the program must inform the trader about it using a message where the excess is described in words (up to 10 points); in other cases, the program must inform that the price does not exceed the predefined level.

Page 1 of 4

Operator 'switch' - Operators - MQL4 Tutorial

case 3 : Alert("Plus three points"); break;

case 4 : Alert("Plus four points"); break;//Here are presented case 5 : Alert("Plus five points"); break;//10 variations 'case', case 6 : Alert("Plus six points"); break;//but, in general case,

case 7 : Alert("Plus seven points"); break;//the amount of variations 'case' case 8 : Alert("Plus eight points"); break;//is unlimited

case 9 : Alert("Plus nine points"); break;

case 10: Alert("Plus ten points"); break;

default: Alert("More than ten points"); // It is not the same as the 'case' } // End of the 'switch' body

//--- return; // Exit start()

}

//---

In this problem solution, we use the operator 'switch', in which each variation 'case' uses the operator 'break'. Depending on the value of variable Delta, the control will be passed to one of variations 'case'. This results in that the operators corresponding with this variation are executed: function Alert() and the operator 'break'. The operator 'break' stops execution of the operator 'switch', i.e., it passes the control outside it, namely, to the operator 'return' that ends the operation of the special function start(). Thus, depending on the value of variable Delta, one of variations 'case' triggers, whereas other variations remain untouched.

The above program is an Expert Advisor, so it will be launched for execution at every tick and it will every time display the messages corresponding with the current situation. Of course, we should search a value for the Level possibly close to the current price of the symbol, the window, to which we are attaching this EA.

Fig. 49. Block diagram of the operator 'switch' in EA pricealert.mq4.

In the block diagram above, we can clearly see that, due to the presence of the operator 'break' in each variation 'case', the control is passed outside the operator 'switch' after the operators of any variation 'case' have been executed. A similar principle of algorithm construction using the operator 'switch' is utilized in the file named stdlib.mq4 delivered within the client terminal (..\experts\libraries\stdlib.mq4).

Let's consider another problem that does not provide the usage of 'break' in each variation 'case'.

It is rather easy to code the solution of this problem (script barnumber.mq4):

//--- // barnumber.mq4

// The code should be used for educational purpose only.

//--- int start() // Special function start()

{

int n = 3; // Preset number

Problem 19. There are 10 bars. Report the numbers of all bars starting with the nth one.

Page 2 of 4

Operator 'switch' - Operators - MQL4 Tutorial

Alert("Bar numbers starting from ", n,":");// It does not depend on n //--- switch (n) // Header of the operator 'switch' { // Start of the 'switch' body case 1 : Alert("Bar 1"); // Variations..

case 2 : Alert("Bar 2");

case 3 : Alert("Bar 3");

case 4 : Alert("Bar 4"); // Here are 10 variations ..

case 5 : Alert("Bar 5"); // ..'case' presented, but, in general, ..

case 6 : Alert("Bar 6"); // ..the amount of variations..

case 7 : Alert("Bar 7"); // ..'case' is unlimited case 8 : Alert("Bar 8");

case 9 : Alert("Bar 9");

case 10: Alert("Bar 10");break;

default: Alert("Wrong number entered");// It is not the same as the 'case' } // End of the 'switch' body

//--- return; // Operator to exit start()

}

//---

In the operator 'switch', the program will search in the variations 'case', until it detects that the Expression is equal to the Constant. When the value of the Expression (in our case, it is integer 3) is equal to one of the Constants (here, it is case 3), all operators that follows the colon (case 3:) will be executed, namely: the operator of function call Alert("Bar 3"), those following Alert("Bar 4"), Alert("Bar 5"), etc., until it comes to the operator 'break' that terminates the operation of the operator 'switch'.

If the value of the Expression does not coincide with any of the Constants, the control is passed to the operator that corresponds to the variation 'default':

Fig. 50. Block diagram of the operator 'switch' in script barnumber.mq4.

Unlike the algorithm realized in the preceding program, in this case (Fig. 50), we are not using the operator 'break' in each variation 'case'. So, if the value of the Expression is equal to the value of one of the Constants, all operators will be executed starting with the operators of the corresponding variation 'case'. We also use the operator 'break' in the last variation 'case' for another purpose: to prevent the operators corresponding with the variation 'default' from executing. If there is no value equal to the Expression among the values of the Constants, the control will be passed to the operator that corresponds with the 'default' label.

Thus, if the value of the preset variable n is within the range of values from 1 to 10, the numbers of all bars will be printed starting with the nth one. If the value of n is beyond the above range, the program will inform the user about no matches.

Page 3 of 4

Operator 'switch' - Operators - MQL4 Tutorial

Please note: It is not necessary that the Constants of the variations 'case' are arranged in your program in order of magnitude. The order of how the variations 'case' with the corresponding Constants follow each other is determined by the needs of the algorithm of your program.

Operator 'continue' Function Call

Page 4 of 4

Operator 'switch' - Operators - MQL4 Tutorial

In document MQL4 Book - PDF format (Page 94-98)