• No results found

General result (the value of variable Res_2) is integer 3 (three)

In document mql4-book (1) (Page 36-41)

Operations and Expressions

Solution 5.2. This solution is wrong:

3. General result (the value of variable Res_2) is integer 3 (three)

In the above example, we consider just a small code fragment, in which the values of the variables of int type are calculated. If we replace these variables with constants with the same values, the final result will be the same. When calculating expressions containing integers, you must pay greater attention to the contents of your program lines. Otherwise, an error may occur in your code, which would be very difficult to find and fix later (especially in large programs). Such troubles do not occur in calculations using real numbers. However, if you use operands of different types in one complex expression, the final result may fully depend on a randomly formed fragment containing division of integers.

In the section of Operators, the term and general properties of operators are considered, whereas the special properties of each operator are described in the chapter named Operators.

Operators

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

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:

Day_Next= TimeDayOfWeek(Mas_Big[n][0]+60); // Simple operator

Go_My_Function_ind(); // Simple operator

a=3; b=a*x+n; i++; // Several operators in a line

Print(" Day= ",TimeDay(Mas_Big[s][0]), // One operator..

" Hour=",TimeHour(Mas_Big[s][0]), // is located..

" Minute=",TimeMinute(Mas_Big[s][0]),// in several ..

" Mas_Big[s][0]= ",Mas_Big[s][0], // lines " Mas_Big[s][1]= ",Mas_Big[s][1]);

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:

// Example of operator switch switch(ii) // Operator switch(expression) { // Opening brace

case 1: Buf_1[Pok-f+i]= Prognoz; break; // Nested operators (operator body)

case 2: Buf_2[Pok-f+i]= Prognoz; break; // Nested operators (operator body)

case 3: Buf_3[Pok-f+i]= Prognoz; break; // Nested operators (operator body)

} // Closing brace,..

// .. shows where the compound

Numb = Numb + Y_raz[tt]*X_raz[ii][tt]; // Nested operator (operator body)

Sred =(Nabor_Koef[ii][vv][2]+ NBh)*Point;// Nested operators (operator body)

The body of a compound is always enclosed in braces and can consist of zero, one, or several operators.

Examples of simple operators:

---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:

{ // Opening brace Day_Next= TimeDayOfWeek(Mas_Big[n][0]+60); // Simple operator b=a*x+n; // Simple operator } // Closing brace..

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.

In document mql4-book (1) (Page 36-41)