跳至内容

Bool Type

bool 类型

bool 类型用于存储逻辑值“真”或“假”,其数值表示为 1 或 0。

示例:

bool a = true;
bool b = false;
bool c = 1;

其内部表示是一个 1 字节的整数。需要注意的是,在逻辑表达式中可以使用其他整数或实数类型及其表达式——编译器不会生成任何错误。在这种情况下,零值将被解释为“假”,所有其他值将被解释为“真”。

示例:

int i=5;
   double d=-2.5;
   if(i) Print("i = ",i," and is set to true");
   else Print("i = ",i," and is set to false");

   if(d) Print("d = ",d," and has the true value");
   else Print("d = ",d," and has the false value");

   i=0;
   if(i) Print("i = ",i," and has the true value");
   else Print("i = ",i," and has the false value");

   d=0.0;
   if(d) Print("d = ",d," and has the true value");
   else Print("d = ",d," and has the false value");

//--- Execution results
//   i= 5 and has the true value
//   d= -2.5 and has the true value
//   i= 0 and has the false value
//   d= 0 and has the false value

另请参阅

布尔运算优先级规则

最后更新于