The Term of Operator
One of the key notions of any programming language is the term of 'operator'. Coding seems to be impossible for the person that has not completely learned this term. The sooner and more properly a programmer learns what operators are and how they are applied in a program, the sooner this programmer starts writing his or her own programs.
Operator is a part of a program; it is a phrase in an algorithmic language that
prescribes a certain method of data conversion.
Any program contains operators. The closest analogy to 'operator' is a sentence. Just as normal sentences compose the text of a story or of a novel, so operators compose a program.
Properties of Operators
There are two kinds of properties of operators - a common property and specific properties of different operators.
Common Property of Operators
All operators have one common property - they all are executed.
We can say that operator is an instruction containing the guide to operations (the description of an order). For a computer, executing a program running on it means
(consecutively passing from one operator to another) complying the orders (prescriptions, instructions) contained in the operators.
Operator as such is just a record, a certain sequence of characters. Operator does not have any levers, wires or memory cells. This is why, when a computer is executing a program, nothing happens in operators as such, they continue staying in the program as composed by the programmer. However, the computer that has all those memory cells and links between them experiences all the transformations inside. If your PC has executed some transformations of data according to the instruction contained in an operator, you say: the operator has been executed.
Specific Properties of Operators
There are several kinds of operators. Operators of each type have their specific properties. For example, the property of an assignment operator is its ability to assign a certain value to the given variable; the property of a loop operator is its multiple executions, etc. Specific properties of operators are considered in all details in the corresponding sections of chapter Operators
in this book. We will only say here that all types of operators have their own properties that are typical only for their type and are not repeated in any other types.
Types of Operators
There are two types of operators: simple operators and compounds.
Simple operators in MQL4 end with the character ";" (semicolon). Using this separator, the PC can detect where one operator ends and another one starts. Character ";" (semicolon) is as necessary in a program as character "." (full stop)
is necessary in a normal text to separate sentences. One operator may take several lines. Several operators may be placed in one line.
||Every simple operator ends with character ";" (semicolon).
Examples of simple operators:
a=3; b=a*x+n; i++;
Print(" Day= ",TimeDay(Mas_Big[s]),
" Mas_Big[s]= ",Mas_Big[s],
" Mas_Big[s]= ",Mas_Big[s]);
Compound Operators (Compounds)
A compound operator consists of several simple ones separated by character
";" and is enclosed in braces. In order to be able to use several operators where only one is expected to be located, programmers use a compound operator (they also call it "block" or "block of code").
The list of operators in a compound is separated by braces. The presence of a closing brace marks the end of a compound operator.
||An exemplary use of a compound in a conditional operator. It starts with the conditional operator if(expression) followed by a compound.
The compound operator contains a list of executable operators.
Fig. 17. Compound operator.
||The body of a compound operator is enclosed in braces. Every compound operator ends with a closing brace.
Examples of compound operators:
case 1: Buf_1[Pok-f+i]= Prognoz; break;
case 2: Buf_2[Pok-f+i]= Prognoz; break;
case 3: Buf_3[Pok-f+i]= Prognoz; break;
for (tt=1; tt<=Kol_Point; tt++)
Numb = Numb + Y_raz[tt]*X_raz[ii][tt];
if (TimeDay(Mas_Big[f])!= 6)
Sred =(Nabor_Koef[ii][vv]+ NBh)*Point;
Ind = Nabor_Koef[ii][vv] + f;
Print(" Ind= ",Ind);
||The body of a compound is always enclosed in braces and can consist of zero, one, or several operators.
Examples of simple operators:
for (n=1; n<=Numb; n++)
Mas[n]= Const_1+ n*Pi;
if (Table > Chair)
Norma = true;
Norma = false;
||Several simple operators may be combined in a compound operator without any strict necessity.
This is a rare, but absolutely admissible construction. In this case, the operators enclosed in braces are called "a block of operators". This use is fully acceptable. This is the programmer who decides whether to enclose operators in braces or not, just for the sake of convenient code representation.
An exemplary block of operators:
Requirements for Operators
Operators must be written in the text of a program according to the format rules (how they must be represented in a code). No operator may be composed beyond these rules. If the program contains an operator composed against the format rules, MetaEditor will produce an error message at compilation.
This means that the program containing the wrong operator cannot be used.
You should understand the phrase "operator format" as a set of format rules typical for the given type of operator. Each type of operator has its own format. Operator formats are in all details considered in the corresponding sections in chapter Operators of this book.
Order of Operators Execution
A very important characteristic of any program is the order of operators execution in it. Operators cannot be executed for no reason or by exception. In MQL4, the following order of operators execution is accepted:
||Operators are executed in the order, in which they occur in the program.
The direction of operators execution is accepted as left to right and downwards.
This means that both simple and compound operators are executed just one by one (like the lines in poems: first we read the top line, then the next lower, then the next, and so on). If there are several operators in one line, they should be executed consecutively, one by one, left to right, then operators are executed in the nearest lower line in the same order.
Operators composing a compound operator are executed in the same way:
any operator in the block of code starts being executed only after the previous one has been executed.
Writing and Execution of Operators: Examples
The text of a program containing operators is not dissimilar in aspect to a normal text or a mathematical notation. However, this similarity is only formal. A normal text allows the notes to be placed in any sequence, whereas you must keep a well-defined order in a program.
As an example, let's see how an assignment operator works.
We'll solve a simple linear equation system and compare the representation of some mathematical calculations in a normal text and in a program code, in operators.
||Problem 7. We have an equation system:
Y = 5
Y - X = 2
The numeric value of variable X is to be found.
Solution 1. A normal text on a sheet of paper:
1. 5 - Х = 2
2. Х = 5 - 2
3. Х = 3
Solution 2. A text in a program:
Y = 5;
X = Y - 2;
Both in the first and in the second solutions, the notes (lines) have a completed content. Nevertheless, the lines in Solution 1 cannot be used in a program as they are, because their appearance does not comply with the assignment operator format.
The notes given in Solution 1 represent some dependences in paper form. They can only be used to inform the programmer about the relations between the variables. Operators in a program are assigned for other purposes - they inform the machine what operations and in what order it must execute. All operators without any exceptions represent precise instructions that allow no ambiguities.
Both operators in Solution 2 are the assignment operators. Any assignment operator gives the machine the following order, literally:
||Calculate the value of the expression to the right of the equality sign and assign the obtained value to the variable to the left of the equality sign.
For this reason, nothing else but variable can be located to the left of the equality sign in an assignment operator. For example, a record of 5 - Х = 2
used in the first solution contains an error, because the set of characters 5 - Х is not a variable. This is why there is no memory cell for placing the numeric value of the expression calculated to the right of the equality sign.
Let's follow the computer during execution of operators of the second solution.
1. Passing on the operator (line 1).
Y = 5;
2. Referencing to the right part of the operator (the right part is located between the equality sign and the semicolon).
3. The computer has detected that the right part of the operator contains a numeric value.
4. Recording the numeric value (5) in the memory cell of variable Y.
5. Passing on the next operator (line 2).
X = Y - 2;
6. Referencing to the right part of the operator.
7. The computer has detected that the right part of the operator contains an expression.
8. Calculating the numeric value of the right part of the operator (5 - 2).
9. Recording the numeric value (3) in the memory cell of variable Х.
Performing steps 1-4 by the computer is execution of the first operator (line 1). Performing steps 5-9 by the computer is execution of the second operator (line 2).
In order to code a workable program, the programmer must well realize what and in what order will be executed in this program.
Particularly, not all mathematical calculations will be put in a program,
it is sometimes necessary to pre-prepare operators.
For example, many intermediate calculations are made when solving mathematical problems. They can help a mathematician to find a proper solution, but turn out to be useless from the viewpoint of programming. Only meaningful solutions can be included into a program, for example: original values of variables or formulas to calculate the values of other variables. In the preceding example, the first operator bears information about the numeric value of variable Y, and the second operator provides the formula to calculate the value of the variable X we are interested in.
Any workable program contains expressions of a familiar sight,
but you can also find such expressions that you will be able to understand only if you rate them as possible operators in your program. For example, the record below
X = X + 1;
seems to be wrong from the standpoint of mathematical logic and common sense. However, it is quite acceptable if you consider it as an operator
(by the way, it is this operator that is widely used in coding).
In this operator, we calculate a new value of variable X: when executing the assignment operator (i.e., calculating the value of the right part of the operator), the computer refers to the memory cell containing the numeric value of variable X (for example, it turns out to be equal to 3 at the moment of referring to it), calculates the expression in the right part of the assignment operator (3 + 1), and writes the obtained value (4) in the memory cell provided for variable X. Execution of this assignment operator results in that the variable X gets the new value (4). The machine will store this value of variable X until the variable X occurs in the left part of the equality sign in another assignment operator. In this case, the new value of this variable will be calculated and stored up to the next possible change.