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

shanmugapradeep

Active Trader
Dec 18, 2020
158
12
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
}
 
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