MQL4参考 语言基础 操作符 假设操作符if-else

If-Else Conditional Operator

The IF - ELSE operator is used when a choice must be made. Formally, the syntax is as follows:

if (expression)

If the expression is true, operator1 is executed and control is given to the operator that follows operator2 (operator2 is not executed). If the expression is false, operator2 is executed.

The else part of the if operator can be omitted. Thus, a divergence may appear in nested if operators with omitted else part. In this case, else addresses to the nearest previous if operator in the same block that has no else part.


//--- The else part refers to the second if operator:
   if(y==2) z=5;
else     z=6;
//--- The else part refers to the first if operator:
   if(y==2) z=5;
else        z=6;
//--- Nested operators
else if(x=='b')
else if(x=='c')
else Print("ERROR");

See also

Initialization of Variables, Visibility Scope and Lifetime of Variables, Creating and Deleting Objects