设为首页 加入收藏

TOP

5.2 Relational and Logical Operators (2)
2013-10-07 15:23:31 来源: 作者: 【 】 浏览:60
Tags:5.2 Relational and Logical Operators

Logical NOT Operator

The logical NOT operator (!) treats its operand as a condition. It yields a result that has the opposite truth value from its operand. If the operand eva luates as nonzero, then ! returns false. For example, we might determine that a vector has  elements by applying the logical NOT operator to the value returned by empty:

  1. // assign value of first element in vec to x if there is one  
  2. int x = 0;  
  3. if (!vec.empty())  
  4. x = *vec.begin(); 

The subexpression

  1. !vec.empty() 

eva luates to true if the call to empty returns false.

一般把 “!” 读做 not,if(!vec.empty()) 读做 if notvec.empty(),表示 if vec is not empty。

The Relational Operators Do Not Chain Together

The relational operators (<, <=, >, <=) are left associative. The fact that they are left associative is rarely of any use because the relational operators return bool results. If we do chain these operators together, the result is likely to be surprising:

  1. // oops! this condition does not determine if the 3 values are unequal  
  2. if (i < j < k) { /* . . . */ } 

As written, this expression will eva luate as true if k is greater than one! The reason is that the left operand of the second less-than operator is the true/false result of the first—that is, the condition compares k to the integer values of 0 or 1.
To accomplish the test we intended, we must rewrite the expression as follows:

  1. if (i < j && j < k) { /* . . . */ } 

Equality Tests and the bool Literals

As we’ll see in Section 5.12.2 (p. 180) a bool can be converted to any arithmetic type—the bool value false converts to zero and true converts to one.

Because bool converts to one, is almost never right to write an equality test that tests against the bool literal true:

  1. if (val == true) { /* . . . */ } 

Either val is itself a bool or it is a type to which a bool can be converted. If val is a bool, then this test is equivalent to writing

  1. if (val) { /* . . . */ } 

which is shorter and more direct (although admittedly when first learning the language this kind of abbreviation can be perplexing).

More importantly, if val is not a bool, then comparing val with true is equivalent to writing

  1. if (val == 1) { /* . . . */ } 

which is very different from

  1. // condition succeeds if val is any nonzero value  
  2. if (val) { /* . . . */ } 

in which any nonzero value in val is true. If we write the comparison explicitly, then we are saying that the condition will succeed only for the specific value 1.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.2 Relational and Logical Oper.. 下一篇5.2 Relational and Logical Oper..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)