跳至内容

Boolean Operations

布尔运算

逻辑否定 NOT (!)

逻辑否定的操作数必须是算术类型。如果操作数的值为 FALSE (0),则结果为 TRUE (1);如果操作数与 FALSE (0) 不同,则结果等于 FALSE (0)。

if(!a) Print("not 'a'");

逻辑或 OR (||)

x 和 y 值的逻辑或运算。如果 x 或 y 的值为真(非空),则表达式值为 TRUE (1);否则为 FALSE (0)。

if(x<0 || x>=max_bars) Print("out of range");

逻辑与 AND (&&)

x 和 y 值的逻辑与运算。如果 x 和 y 的值都为真(非空),则表达式值为 TRUE (1);否则为 FALSE (0)。

布尔运算的简要估算

所谓的“简要估算”方案适用于布尔运算,即当表达式的结果可以精确估计时,计算即终止。

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- the first example of the brief estimate
   if(func_false() && func_true())
     {
      Print("Operation &&: You will never see this expression");
     }
   else
     {
      Print("Operation &&: Result of the first expression is false, so the second wasn't calculated");
     }
//--- the second example of the brief estimate
   if(!func_false() || !func_true())
     {
      Print("Operation ||: Result of the first expression is true, so the second wasn't calculated");
     }
   else
     {
      Print("Operation ||: You will never see this expression");
     }
  }
//+------------------------------------------------------------------+
//| the function always returns false                                |
//+------------------------------------------------------------------+
bool func_false()
  {
   Print("Function func_false()");
   return(false);
  }
//+------------------------------------------------------------------+
//| the function always returns true                                 |
//+------------------------------------------------------------------+
bool func_true()
  {
   Print("Function func_true()");
   return(true);
  }

另请参阅

优先级规则

最后更新于