Syntax

I hope you enjoyed the "Welcome" lesson that I tried to answer the very basic questions: what MQL4 is, why MQL4 and where (to write) MQL4?
But the most important question has not been answered yet: How to write MQL4. All of the coming lessons are attempts of answering this question.
Now I want you to relax and empty your mind for the coming few minutes.
We are talking today about the SYNTAX rules of MQL4.
And as I told you before, if you are coming from C/C++ programming then you know a lot of MQL4 before I start my lessons because the syntax of MQL4 language is very similar to the syntax of C/C++ language.


What does "SYNTAX" mean?

The dictionary meaning of the word Syntax of a programming language is: "The set of allowed reserved words and their parameters and the correct word order in the expression is called the syntax of language" . wordiq

The syntax of the programming langauge is equal to the grammer to the humen speaking language.

So, when we study the syntax of a programming language we are studding its grammar and witting rules that consists of:

• Format
• Comments
• Identifiers
• Reserved words

let's slice the cake

1- Foramt:

When you write you MQL4 code, you can freely use any set of spaces, tabs and empty lines to separate your code and you lines of code to make them readable and eyes pleasing.
For example all of these lines are valid in MQL4:

1
double MacdCurrentMacdPreviousSignalCurrent;
1
2
3
4
double
MacdCurrent,
MacdPrevious,
SignalCurrent;
1
double     MacdCurrent,     MacdPrevious,     SignalCurrent;

But, as you see, the first line is more readable and easy to understand.
And like everything in the life There are Exceptions to every general rule:

1- You can't use a new line in the middle of "Controlling Compilation".
You will know more about "Controlling Compilation" in the next lesson but just remember this is an exception.
For example the next line of code is invalid in MQL4 and the compiler will complain:

1
2
#propery
copyright "Copyright © 2005, MetaQuotes Software Corp."

#property copyright is a Controling compilation so the line should be like this:

1
#propery copyright "Copyright © 2005, MetaQuotes Software Corp."

2- You can't use a new line of a space in the middle of Constant values, Identifiers or Keywords.
For example this line is a valid:

1
extern int MA_Period=13;

"extern" and "int" in the above code are Keywords. "MA_Period" is an indentifier and "13" is a Constant value.
You will know more about them in the next lessons.
For example the next lines are invalid lines:

1
extern int MA_   Period=13;
1
extern int MA_Period=1             3;
1
2
ex
tern int MA_Period=13;

2- Comments:

To make the programming world easier, any programming language has its style of writing comments.
We use Comments to write lines in our code that the compiler will ignore them when it compiles our program. But we write them to make our code clearer and understandable.

Assume that you wrote a program in the last summer and in the winter you opened it and start to read the code. Without Comments - even you are the code's creator - you can't understand all those puzzled lines.
MQL4 uses two styles of Comments:

1- Single line comments:

The Single line comments start with "//" and end with the end of the line. For example:

1
2
//This is a Single line comment.
extern int MA_Period=13;
1
extern int MA_Period=13;   //This is another comment.

2- Multiline comments:

The multiline comments start with "/*" and end with "*/". You can comment a line, piece of line or multi-lines buy putting "/*" at the start and "*/" at the end of block you want to comment.

1
2
3
4
/* This
is
multiline
comment */

You can also nest signle line comments inside the multiline comments like this:

1
2
3
4
/* This
is
multiline  //another comment nested here
comment */

This is a valid comment too:

1
extern int /*HELLO I'm a comment*/ MA_Period=13;

But this invalid comment:

1
extern int //HELLO I'm a comment MA_Period=13;

3- Identifiers:

An identifier is the name you give to a Variable, a Constant or a Function.
For example MA_Period here is an identifier because it's a variable name.

1
extern int MA_Period=13;   //MA_Period is an identifier

There are few rules and restrictions for choosing the identifiers names:

1- The length of the identifier must not exceed 31 characters.

2- The identifier must begin with a letter (capital or small) or the underline symbol "_".
It not allowed to start with a number or any other symbol except the underline.

3- You can use only letters, number and underline symbol.

4- You can't use any of reserved words as identifier. You will find a list of reserved words later in this lesson.

5- The identifiers' name are case sensitive.
So, MA_PERIOD is not the same as ma_period or MA_Period.

Let's take some examples:

1
2
3
4
5
6
7
_Name1    Valid
1Name1    Invalid (Dont start with a number)
~Name1    Invalid (You can only start with underline no other symbols)
N~ame1    Invalid (You can use only lettersnumbers or underline)
i_love_my_country_and_my_country_love_all_the_world    Invalid (more than 31 characters)
Color    Valid
color    Invalid (you cant use reserved words (color with a small c)

4- Reserved words:

There are "words" that the language uses them for specific actions.
So, they are reserved to the language usage and you can't use them as an identifier name. 
This is the list of the reserved words (from the MQL4 guide):

This is the list of the reserved words (from the MQL4 guide):

Data types

Memory classes

Operators

Other

bool

extern

break

false

color

static

case

true

datetime

 

continue

 

double

 

default

 

int

 

else

 

string

 

for

 

void

 

if

 

 

 

return

 

 

 

switch

 

 

 

while

 


For example the next lines are invalid lines of code:

1
2
3
extern int datetime=13;
int extern=20;
double continue =0;

I hope you enjoyed the lesson. The next lesson is about the "Data Types".
So, Be Ready, the hard work is coming!

See you
Coders’ Guru

Do You Like this Article?