MQL4 If & Else Conditional Operators

We use the conditional operators If and Else to construct our trading conditions. Learn in this MQL4 If & Else Conditional Operators article how to program the Expert Advisor to execute trading conditions.

The if operator evaluates a true and false condition. If the condition is true, the code immediately after the if statement is executed. If the condition is false, it will skip ahead to the code following the if block: 

if (BuyCondition=true)
{OpenBuyOrder(…);
}

If there is only one statement folowing the if operator, it can be written like this: 

if (BuyCondition == true) OpenBuyOrder(…); 

Multiple statements must be enclosed in braces. 

The else operator evaluates an alternative condition, provided that the previous if statement(s) are false. You can combine else and if to create an alternative condition that will only be executed if it is true. 

For example, this code evaluates three conditions in order. If one of them is true, only that block of code will be executed. If none of them are true, none of them will be executed: 

if (Condition1 == true) // execute condition1
else if (Condition2 = true) // execute condition2
else if (Condition3 = true) // execute condition3

The else operator can be used by itself at the end of an if-else sequence to indicate a condition that will be executed by default if all of the other if operators are false.