欢迎光临
我们一直在努力

《C编程语言, 第二版》 第3章:Control Flow 读书笔记

本文为《The C Programming Language, 2nd Edition》第3章:Control Flow 的读书笔记。

The control-flow statements of a language specify the order in which computations are performed. 语言的控制流语句规定了计算执行的顺序。

3.1 Statements and Blocks

In C, the semicolon is a statement terminator 在C语言中,分号是语句结束符

Braces { and } are used to group declarations and statements together into a compound statement, or block, so that they are syntactically equivalent to a single statement. There is no semicolon after the right brace that ends a block. 大括号 { 和 } 用于将声明和语句组合在一起形成复合语句或块,从而在语法上等同于单个语句。结束一个块的右大括号后不需要分号。

3.2 If-Else

语法如下,其中else部分可以省略:

if (expression)
statement1
else
statement2

if (expression)等同于if (expression != 0)

it’s a good idea to use braces when there are nested ifs. 当存在嵌套的 if 语句时,使用大括号是个好主意。

3.3 Else-If

语法如下:

if (expression)
statement
else if (expression)
statement
else if (expression)
statement
else if (expression)
statement
else
statement

This sequence of if statements is the most general way of writing a multi-way decision. The expressions are evaluated in order; if any expression is true, the statement associated with it is executed, and this terminates the whole chain. 这组 if 语句是编写多路决策的最通用方法。表达式按顺序求值;如果任何表达式为真,则执行与之关联的语句,并且这会终止整个链。

The last else part handles the “none of the above” or default case where none of the other conditions is satisfied. … can be omitted 最后的 else 部分处理“以上都不是”或默认情况,即当没有其他条件满足时的情况 … 可以省略。

3.4 Switch

语法如下:

switch (expression) {
case constexpr : statements
case constexpr : statements
default: statements
}

Each case is labeled by one or more integer-valued constants or constant expressions. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases are satisfied. A default is optional; if it isn’t there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order. 每个 case 都由一个或多个整数常量或常量表达式标记。如果某个 case 与表达式的值匹配,则从该 case 开始执行。所有 case 表达式必须不同。标记为 default 的 case 会在没有其他 case 满足条件时执行。default 是可选的;如果没有 default,并且没有任何 case 匹配,则不会执行任何操作。case 和 default 子句可以以任意顺序出现。

Because cases serve just as labels, after the code for one case is done, execution falls through to the next unless you take explicit action to escape. break and return are the most common ways to leave a switch. 因为 case 仅仅作为标签使用,所以在一个 case 的代码执行完毕后,除非你采取明确的措施退出,否则执行会继续流向下一个 case。break 和 return 是最常见的离开 switch 的方式。

Falling through cases is a mixed blessing. On the positive side, it allows several cases to be attached to a single action, as with the digits in this example. But it also implies that normally each case must end with a break to prevent falling through to the next. Falling through from one case to another is not robust, being prone to disintegration when the program is modified. With the exception of multiple labels for a single computation, fall-throughs should be used sparingly, and commented. 在案例中连续执行(falling through)是一把双刃剑。从积极的方面看,它允许多个案例附加到一个动作上,就像本例中的数字一样。但这也意味着通常每个案例必须以 break 结束,以防止落入下一个案例。一个案例跌入另一个案例的方式并不稳健,在程序修改时容易出问题。除了为单个计算使用多个标签外,应谨慎使用连续执行,并添加注释。

示例:

while ((c = getchar()) != EOF) {
switch (c) {
case0: case1: case2: case3: case4:
case5: case6: case7: case8: case9:
ndigit[c0]++;
break;
case ′ ′:
case ′\\n′:
case ′\\t′:
nwhite++;
break;
default:
nother++;
break;
}

As a matter of good form, put a break after the last case (the default here) even though it’s logically unnecessary. 从良好编程规范的角度来看,即便逻辑上并非必需,也应在最后一个 case(这里是 default)之后加上一条 break。

3.5 Loops—While and For

while的语法:

while (expression)
statement

for的语法:

for (expr1; expr2; expr3)
statement

等同于:

expr1;
while (expr2) {
statement
expr3;
}

for语句三部分中的任何一部分都可以省略,但分号必须保留。例如for (;;)

使用 while 还是 for 在很大程度上取决于个人偏好。

The for is preferable when there is a simple initialization and increment, since it keeps the loop control statements close together and visible at the top of the loop. 当初始化和自增操作较为简单时,建议使用 for 循环,因为它可以将循环控制语句集中在一起,并且在循环顶部就能看到。

Because the components of the for are arbitrary expressions, for loops are not restricted to arithmetic progressions. Nonetheless, it is bad style to force unrelated computations into the initialization and increment of a for, which are better reserved for loop control operations. 由于 for 循环的组件可以是任意表达式,因此 for 循环并不限于算术级数。然而,将无关的计算强行放入 for 的初始化和增量中是糟糕的风格,这些部分更适合用于循环控制操作。

The advantages of keeping loop control centralized are even more obvious when there are several nested loops. 当存在多个嵌套循环时,将循环控制集中管理的好处更加明显。

One final C operator is the comma “,”, which most often finds use in the for statement. A pair of expressions separated by a comma is evaluated left to right, and the type and value of the result are the type and value of the right operand. 最后一个 C 操作符是逗号 “,”,它最常用于 for 语句中。一对用逗号分隔的表达式会从左到右进行求值,其结果的类型和值是右操作数的类型和值。

The commas that separate function arguments, variables in declarations, etc., are not comma operators, and do not guarantee left to right evaluation. 分隔函数参数、声明中的变量等的逗号,并不是逗号运算符,也不能保证从左到右的求值顺序。

逗号运算符应谨慎使用。最合适的用法是在强相关的结构中,例如逆序的 for 循环,以及在宏中必须将多步骤计算作为单一表达式的情况。在逆序交换元素时,逗号表达式也可能是合适的,这里的交换可以被视为一个单一操作:

for (i = 0, j = strlen(s)1; i < j; i++, j)
c = s[i], s[i] = s[j], s[j] = c;

3.6 Loops—Do-while

语法如下:

do
statement
while (expression);

Experience shows that do-while is much less used than while and for. 经验表明,do-while 的使用比 while 和 for 少得多。

3.7 Break and Continue

It is sometimes convenient to be able to exit from a loop other than by testing at the top or bottom. The break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost enclosing loop or switch to be exited immediately. 有时能够通过除在循环顶部或底部进行测试之外的方式退出循环是很方便的。break语句提供了从for、while和do循环中提前退出的方法,就像从switch中退出一样。break会立即导致最内层的循环或switch被退出。

The continue statement is related to break, but less often used; it causes the next iteration of the enclosing for, while, or do loop to begin. In the while and do, this means that the test part is executed immediately; in the for, control passes to the increment step. The continue statement applies only to loops, not to switch. A continue inside a switch inside a loop causes the next loop iteration. continue 语句与 break 相关,但使用频率较低;它会导致包含它的 for、while 或 do 循环开始下一次迭代。在 while 和 do 循环中,这意味着测试部分会立即执行;在 for 循环中,控制会传递到增量步骤。continue 语句只适用于循环,不适用于 switch。循环中的 switch 内的 continue 会导致下一次循环迭代。

3.8 Goto and Labels

C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. C 提供了可以被无限滥用的 goto 语句,以及用于跳转的标签。从理论上讲,goto 永远不是必需的,而在实践中几乎总是可以轻松地在不使用它的情况下编写代码。在本书中我们没有使用过 goto。

With a few exceptions like those cited here, code that relies on goto statements is generally harder to understand and to maintain than code without gotos. Although we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all. 除了这里引用的少数例外,依赖于goto语句的代码通常比没有goto的代码更难理解和维护。尽管我们对这个问题并不教条,但似乎goto语句应该很少使用,甚至最好完全不用。

赞(0)
未经允许不得转载:171主机测评 » 《C编程语言, 第二版》 第3章:Control Flow 读书笔记
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址