Let's hope that soon we will have an opportunity to experiment not only with the programming language, but also seen in the trading terminal MetaTrader 4 complex written program in place.
Now forget about this, of course, exciting news and will return to the study of MQL 4. Proceed to consider the operator of choice "switch".
In the last issue of the journal Forex Magazine reviewed conditionals "if". In the same place it was observed that the algorithms to build their programs so as to avoid the use of the second part of "else" operator "if". But in practice it is not always possible to achieve that, moreover, are often situations when the condition operator "if" it is not clear to the expression that gives one of two outcomes "true" or "false".
The simplest explanation is an example. Suppose we know that some of the variable takes a few well-defined values. We should check whether the variable is each of the predefined values, and, depending on the test results, perform different actions. You can see that the operator, which allows you to do one of two actions (depending on the conditions) are not entirely suitable. That is, when it comes to make multiple selections, classic version of the operator "if" - not the best solution. You can, of course, to use a series of nested operators "if", which would be as follows:
if (my_var == condition1) (
/ / If the following first
/ / Condition, then perform
/ / This block
) Else (
if (my_var == condition2) (
/ / If the following second
/ / Condition, then perform
/ / This block
) Else (
if (my_var == condition3) (
/ / If the third
/ / Condition, then perform
/ / This block
) Else (
/ / If not done
/ / None of the three
/ / Previous conditions,
/ / Execute this block
)
)
)
We see that when the variable my_var checked for a match with only three different values, use the operator "if" makes some clutter in the code of the program: first, it is very easy to get to comprehend the brackets, and secondly, the code has become unwieldy, difficult to understand, and, moreover, the search for errors in the code can be a lot of trouble. Imagine what will be happening in the case where the variable will be checked for a match with ten different values.
Fortunately, for those cases in MQL 4 operator added "switch". Applied to the situation described above, using the operator "switch" looks like this:
switch (my_var) (
case condition1: (
/ / If the following first
/ / Condition, then perform
/ / This block
) Break;
case condition2: (
/ / If the following second
/ / Condition, then perform
/ / This block
) Break;
case condition3: (
/ / If the third
/ / Condition, then perform
/ / This block
) Break;
default: (
/ / If not done
/ / None of the three
/ / Previous conditions,
/ / Execute this block
)
)
This code is easier to read. Now, explain syntax of "switch", and how this operator works.
Just for the keyword "switch" in parentheses should be variable, a value which will be compared with several meanings. Following this, the operator in parentheses placed options "case" (one or more), each of which contains the verifiable value. After the program, go to the construction of "switch ... case", there has been a consistent comparison of the variable specified after the keyword "switch" with the values following the keyword "case".
At the very end of comparison may attend optional "default". If the program fails to comply with any of the blocks of "case", then it goes to the block "default". It should be read as a block, running by default, that is executed when there are no blocks of "case", the processing of the situation.
Please note that each block of "case" ends with the keyword "break". If you do not put at the end of the processing unit "case" operator "break", it will be performed next block "case" and "default" without any checks and until not meet "break" or not to finish the scope of the operator "switch". This nuance has its application in cases where multiple values of the target variable should be compared the same handler.
switch (my_var) (
case condition1:
case condition2: (
/ / Execute this block,
/ / If the following first
/ / Or the second condition.
) Break;
case condition3: (
/ / Execute this block,
/ / If the third
/ / Condition.
) Break;
)
Due to the fact that MetaEditor 4 became available the masses, we can look at examples of programs that comes with it. Using operator "switch" is illustrated in the display "Moving Average 4. The following code shows how to calculate different types of MA is considered by us in this article, the operator of "switch".
/ / Calculate current moving
/ / Average value
switch (g_MAType) (
case 0:
CalculateSMA ();
break;
case 1:
CalculateEMA ();
break;
case 2:
CalculateSMMA ();
break;
case 3:
CalculateLWMA ();
break;
)
Alexander Ivanov
to Forex Magazine
to Forex Magazine
No comments:
Post a Comment