Friday, February 27, 2009

Programming in MQL 4: Conditional operators

Today, as promised last issue of the journal Forex Magazine, we look at another missing link to create a full program: "conditionals."

If you try to draw parallels between the situations that arise in programming, and daily life, the "conditional operator" could be explained by the example of traveler, is at a crossroads and think where to go next. Let's try to imagine a course of reasoning which drive traveler to the choice of the direction of the path. If you omit all the details of the discussion of our "traveler", the course of his thoughts, you can submit a phrase: "if a certain condition, then make such a force, or do another operation." At the moment we are not so important, on what basis he can decide in which direction to continue the further way, because now for us is most important that in such a situation arises the moment of choice. In short, this pattern of behavior is called "if, then, else."

Sooner or later in the algorithm for almost any program is branching further course of action, it just is a time when it is required to determine in which of the areas will continue to move the program. To illustrate the above, the arrangement of the code written on MQL 4:

if (condition) (
/ / Execute this block,
/ / If the condition is true.
) Else (
/ / Execute this block,
/ / If the condition is false.
)

The variable "condition" in this case must be of type "bool". It can take values true or false. In addition, instead of a variable "condition" with an can be any logical expression, which make up by using comparison operators (>,> =, ==, <=, <), the negation operator (!) And the operators and and or. In the case where the variable "condition" or podstavlennoe replaced by a logical expression takes value "true", the code is executed, the prisoner in the first operator brackets. If the variable "condition" or podstavlennoe replaced by a logical expression takes value "false", code is executed, the prisoner in the second operator brackets, those that, after the word "else". Then the variable "condition" or podstavlennoe replaced by a logical expression will be referred to as a condition operator "if". In the previous example, the design is called a complete form of a conditional operator "if () () else ()". However, it is not always necessary to comply with any code, if the condition operator "if" is false. The following example shows the use of a short form of the operator "if () ()": if (condition) ( / / Execute this block, / / If the condition is true. ) Short form of the operator "if" easy to use, when checking the initial parameters kakihlibo functions. For example, the below piece of code shows the initial lines of the function init () Indicator Moving Averages, which occur checking input parameters to construct the indicator. When the billing period for this indicator is less than one bar, or type specified in the initialization does not coincide with any of the four permitted types of MA, the indicator still at a stage of initialization stops its work. if (MAPeriod <1> 3) (

g_Failed = true;
return (0);
)

If all of the checks passed, then no further action need to register in the form of "else" part of the operator, initialize, and in both the case will continue.

Actually, if we want to write fast programs, the full form of the operator "if" should be used as sparingly as possible. This is due to the characteristics of programs at the level of the processor.

To complete the study material should turn our attention to the fact that the condition operator "if" can be used not only the logical expression. If the conditions specify numeric variable or expression, the result of which will be the number of the operator "if" will take all the numbers are different from zero as the value "true", otherwise, if zero, the condition will be false. This can be used in cases where the value of a variable is checked for equality (inequality) to zero.

The following example shows two options for checking the variable equal to zero. Here, both the operator "if" one and the same:

int i = 1234;

/ / First version
if (i! = 0) (
Alert ( "i! = 0");
)

/ / Second version
if (i) (
Alert ( "i! = 0");
)

At the finish we have to consider ways and examples of the use of the conditional operator "if", because its use is an integral part of any more or less-developed program, and in the future, we still have to face him.



Alexander Ivanov

No comments: