设为首页 加入收藏

TOP

5.7 The Conditional Operator
2013-10-07 15:24:24 来源: 作者: 【 】 浏览:67
Tags:5.7 The Conditional Operator

The conditional operator is the only ternary operator in C++(www.cppentry.com). It allows us to embed simple if-else tests inside an expression. The conditional operator has the following syntactic form

  1. cond   expr1 : expr2; 

where cond is an expression that is used as a condition (Section 1.4.1, p. 12). The operator executes by eva luating cond. If cond eva luates to 0, then the condition is false; any other value is true. cond is always eva luated. If it is true, then expr1 is eva luated; otherwise, expr2 is eva luated. Like the logical AND and OR (&& and ||) operators, the conditional operator guarantees this order of eva luation for its operands. Only one of expr1 or expr2 is eva luated. The following program
illustrates use of the conditional operator:

  1. int i = 10, j = 20, k = 30;  
  2. // if i > j then maxVal = i else maxVal = j  
  3. int maxVal = i > j   i : j; 

注意:不要写出 (a > b) true : false 这种多余的条件操作符,直接写 (a > b) 即可。

Avoid Deep Nesting of the Conditional Operator

We could use a set of nested conditional expressions to set max to the largest of three variables:

  1. int max = i > j  
  2.  i > k   i : k  
  3. : j > k   j : k; 

We could do the equivalent comparison in the following longer but simpler way:

  1. int max = i;  
  2. if (j > max)  
  3. max = j;  
  4. if (k > max)  
  5. max = k; 

Using a Conditional Operator in an Output Expression

The conditional operator has fairly low precedence. When we embed a conditional expression in a larger expression, we usually must parenthesize the conditional subexpression. For example, the conditional operator is often used to print one or another value, depending on the result of a condition. Incompletely parenthesized uses of the conditional operator in an output expression can have surprising results:

  1. cout << (i < j   i : j); // ok: prints larger of i and j  
  2. cout << (i < j)   i : j; // prints 1 or 0!  
  3. cout << i < j   i : j; // error: compares cout to int 

The second expression is the most interesting: It treats the comparison between i and j as the operand to the << operator. The value 1 or 0 is printed, depending on whetheri < jis true or false. The << operator returns cout, which is tested as the condition for the conditional operator. That is, the second expression is equivalent to

  1. cout << (i < j); // prints 1 or 0  
  2. cout   i : j; // test cout and then eva luate i or j  
  3. // depending on whether cout eva luates to true or false 

EXERCISES SECTION 5.7

Exercise 5.20: Write a program to prompt the user for a pair of numbers and report which is smaller.

Exercise 5.21: Write a program to process the elements of a vector<int>. Replace each element with an odd value by twice that value.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.8 The sizeof Operator 下一篇5.6 The Arrow Operator

评论

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

·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)