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:
2
3
4
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:
2
#property copyright is a Controling compilation so the line should be like this:
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:
"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:
2
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:
2
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.
2
3
4
You can also nest signle line comments inside the multiline comments like this:
2
3
4
This is a valid comment too:
But this invalid comment:
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.
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:
2
3
4
5
6
7
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:
2
3
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
Tweet
