Chapter 2

来源:互联网 发布:白衬衫 知乎 编辑:程序博客网 时间:2024/06/05 08:30

2.10 Assignment Operators and Expressions

An expression such as

i = i + 2

in which the variable on the left side is repeated immediately on the right, can be written in the compressed form

在赋值表达式中,如果表达式左边的变量重复出现在表达式的右边,如:则可以将这种表达式缩写为下列形式:

i += 2

The operator += is called an assignment operator.

其中的运算符+=称为赋值运算符。 

Most binary operators (operators like + that have a left and right operand) have a corresponding assignment operator op=, where op is one of

大多数二元运算符(即有左、右两个操作数的运算符,比如+)都有一个相应的赋值运算op=,其中,op可以是下面这些运算符之一:

+ - * / % << >> & ^ |

If expr1 and expr2 are expressions, then

如果expr1expr2是表达式,那么 

expr1 op= expr2

is equivalent to

expr1 = (expr1) op (expr2)

except that expr1 is computed only once. Notice the parentheses around expr2:

它们的区别在于,前一种形式expr1只计算一次。注意,在第二种形式中,expr2两边的圆括号是必不可少的,例如,

x *= y + 1

means

x = x * (y + 1)

rather than

x = x * y + 1

As an example, the function bitcount counts the number of 1-bits in its integer argument.

我们这里举例说明。下面的函数bitcount统计其整型参数的值为1的二进制位的个数。 

/* bitcount: count 1 bits in x */

int bitcount(unsigned x)

{

int b;

for (b = 0; x != 0; x >>= 1)

if (x & 01)

b++;

return b;

}

Declaring the argument x to be an unsigned ensures that when it is right-shifted, vacated bits will be filled with zeros, not sign bits, regardless of the machine the program is run on.

这里将x 声明为无符号类型是为了保证将x 右移时,无论该程序在什么机器上运行,左边空出的位都用0(而不是符号位)填补。 

Quite apart from conciseness, assignment operators have the advantage that they correspond better to the way people think. We say ``add 2 to i'' or ``increment i by 2'', not ``take i, add 2, then put the result back in i''. Thus the expression i += 2 is preferable to i = i+2. In addition, for a complicated expression like

除了简洁外,赋值运算符还有一个优点:表示方式与人们的思维习惯比较接近。我们通常会说“把2 加到i 上”或“把i 增加2”,而不会说“取i 的值,加上2,再把结果放回到i中”,因此,表达式i += 2i = i + 2更自然,另外,对于复杂的表达式,例如:

 

yyval[yypv[p3+p4] + yypv[p1]] += 2

the assignment operator makes the code easier to understand, since the reader doesn't have to check painstakingly that two long expressions are indeed the same, or to wonder why they're not. And an assignment operator may even help a compiler to produce efficient code.

赋值运算符使程序代码更易于理解,代码的阅读者不必煞费苦心地去检查两个长表达式是否完全一样,也无须为两者为什么不一样而疑惑不解,并且,赋值运算符还有助于编译器产生高效代码。

 

We have already seen that the assignment statement has a value and can occur in expressions; the most common example is

从上述例子中我们可以看出,赋值语句具有值,且可以用在表达式中。下面是最常见的一个例子:

while ((c = getchar()) != EOF)

...

The other assignment operators (+=, -=, etc.) can also occur in expressions, although this is less frequent.

其它赋值运算符(如+=-=等)也可以用在表达式中,尽管这种用法比较少见。

In all such expressions, the type of an assignment expression is the type of its left operand, and the value is the value after the assignment.

在所有的这类表达式中,赋值表达式的类型是它的左操作数的类型,其值是赋值操作完成后的值.


2.11 Conditional Expressions

 

The statements

if (a > b)

z = a;

else

z = b;

compute in z the maximum of a and b. The conditional expression, written with the ternary operator ``?:'', provides an alternate way to write this and similar constructions. In the expression

用于求ab中的最大值,并将结果保存到z中。条件表达式(使用三元运算符“? :”)提供了另外一种方法编写这段程序及类似的代码段,在表达式中,

expr1 ? expr2 : expr3

the expression expr1 is evaluated first. If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional expression. Otherwise expr3 is evaluated, and that is the value. Only one of expr2 and expr3 is evaluated. Thus to set z to the maximum of a and b,

首先计算expr1,如果其值不等于0(为真),则计算expr2 的值,并以该值作为条件表达式的值,否则计算expr3 的值,并以该值作为条件表达式的值。expr2 expr3 中只能有一个表达式被计算。因此,以上语句可以改写为:

z = (a > b) ? a : b; /* z = max(a, b) */

It should be noted that the conditional expression is indeed an expression, and it can be used wherever any other expression can be. If expr2 and expr3 are of different types, the type of the result is determined by the conversion rules discussed earlier in this chapter. For example, if f is a float and n an int, then the expression

应该注意,条件表达式实际上就是一种表达式,它可以用在其它表达式可以使用的任何地方;如果expr2 expr3 的类型不同,结果的类型将由本章前面讨论的转换规则决定。例如,如果ffloat类型,nint类型,那么表达式

(n > 0) ? f : n

is of type float regardless of whether n is positive.

float类型,与n是否为正值无关。

Parentheses are not necessary around the first expression of a conditional expression, since the precedence of ?: is very low, just above assignment. They are advisable anyway, however, since they make the condition part of the expression easier to see.

条件表达式中第一个表达式两边的圆括号并不是必须的,这是因为条件运算符?:的优先级非常低,仅高于赋值运算符。但我们还是建议使用圆括号,因为这可以使表达式的条件部分更易于阅读。

The conditional expression often leads to succinct code. For example, this loop prints n elements of an array, 10 per line, with each column separated by one blank, and with each line (including the last) terminated by a newline.

采用条件表达式可以编写出很简洁的代码。例如,下面的这个循环语句打印一个数组的n个元素,每行打印10个元素,每列之间用一个空格隔开,每行用一个换行符结束(包括最后一行):

for (i = 0; i < n; i++)

printf("%6d%c", a[i], (i%10==9 || i==n-1) ? '\n' : ' ');

A newline is printed after every tenth element, and after the n-th. All other elements are followed by one blank. This might look tricky, but it's more compact than the equivalent if-else. Another good example is

在每10个元素之后以及在第n个元素之后都要打印一个换行符,所有其它元素后都要打印一个空格。编写这样的代码可能需要一些技巧,但比用等价的if-else结构编写的代码要紧凑一些。下面是另一个比较好的例子:

printf("You have %d items%s.\n", n, n==1 ? "" : "s");




painstakingly  adv. 刻苦地;煞费苦心地

succinct  adj. 简洁的;简明的


原创粉丝点击