[MQL4 Coding Help] How to write If-else sequence in MQL4

shanmugapradeep

Active Trader
Dec 18, 2020
119
5
34
39
Hello,

What is correct way to add if else statement.

TYPE 1 : If else sequence separately :


MQL4:
if (condition1) {
//Condition 1 task
} else {
if (condition2) {
//condition2 task
} else {
if(condition3) {
//condition3 task
} else {
//condition else task
}


TYPE 2 : Else if statement sequence :

MQL4:
if (condition1) {
//Condition 1 task
} else if (condition2) {
//condition2 task
} else if(condition3) {
//condition3 task
} else {
//condition else task
}

TYPE 3 : Else if statement with no space sequence :

MQL4:
if (condition1) {
//Condition 1 task
} elseif (condition2) {
//condition2 task
} elseif(condition3) {
//condition3 task
} else {
//condition else task
}
 

hayseed

Master Trader
Jul 27, 2010
1,114
271
149
usa
Thanks for this URL. So, type 2 is correct statement for nested if else.


And also thanks for this tip. I did not know about it :)
//-----

quite often i will search the mql book...... it is a searchable pdf.......

nested functions are on page 7 and 8......h
//------
 

Attachments

  • mql4 manual.pdf
    840.6 KB · Views: 2
  • 👍
Reactions: Enivid

Enivid

Administrator
Staff member
Nov 30, 2008
19,064
1,473
144
Odesa
www.earnforex.com
TYPE 1 : If else sequence separately :
This won't compile because you have two unbalanced parentheses there:
MQL4:
if (condition1)
{
//Condition 1 task
}
else
{
    if (condition2)
    {
        //condition2 task
    }
    else
    {
        if(condition3)
        {
            //condition3 task
        }
        else
        {
            //condition else task
        }

TYPE 2 : Else if statement sequence :
This will work fine (and will work similarly to the switch operator):
MQL4:
if (condition1)
{
    //Condition 1 task
}
else if (condition2)
{
    //condition2 task
}
else if(condition3)
{
    //condition3 task
}
else
{
    //condition else task
}

TYPE 3 : Else if statement with no space sequence :
MetaTrader doesn't know the elseif statement. You should put a space there.
 
  • 👍
Reactions: shanmugapradeep