Chapter 3 - Control Flow(一)

The control-flow of a language specify the order in which computations are performed. We have already met the most common control-flow constructions in earlier examples; here we will complete the set, and be more precise about the ones discussed before.

程序语言中的控制流语句用于控制各计算操作执行的次序。在前面的例子中,我们曾经使用了一些最常用的控制流结构。本章将更详细地讲述控制流语句。

3.1 Statements and Blocks

An expression such as x = 0 or i++ or printf(...) becomes a statement when it is followed by a semicolon, as in

x = 0;

i++;

printf(...);

In C, the semicolon is a statement terminator, rather than a separator as it is in languages like Pascal.

C语言中,分号是语句结束符,而Pascal 等语言却把分号用作语句之间的分隔符。 

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. The braces that surround the statements of a function are one obvious example; braces around multiple statements after an if, else, while, or for are another. (Variables can be declared inside any block; we will talk about this in Chapter 4.) There is no semicolon after the right brace that ends a block.

用一对花括号“{”与“}”把一组声明和语句括在一起就构成了一个复合语句(也叫作程序块),复合语句在语法上等价于单条语句。函数体中被花括号括起来的语句便是明显一例。ifelsewhilefor之后被花括号括住的多条语句也是类似的例子。(在任何程序块中都可以声明变量,笫4章将对此进行讨论。)右花括号用于结束程序块,其后不需要分号。



3.2 If-Else

The if-else statement is used to express decisions. Formally the syntax is

if (expression)

statement1

else

statement2

where the else part is optional. The expression is evaluated; if it is true (that is, if expression has a non-zero value), statement1 is executed. If it is false (expression is zero) and if there is an else part, statement2 is executed instead.

其中else部分是可选的。该语句执行时,先计算表达式的值,如果其值为真(即表达式的值为非0),则执行语句1;如果其值为假(即表达式的值为0),并且该语句包含else 部分,则执行语句2

Since an if tests the numeric value of an expression, certain coding shortcuts are possible. The most obvious is writing

由于if语句只是简单测试表达式的数值,因此可以对某些代码的编写进行简化。最明显的例子是用如下写法

if (expression)

instead of

if (expression != 0)

Sometimes this is natural and clear; at other times it can be cryptic.

某些情况下这种形式是自然清晰的,但也有些情况下可能会含义不清。 

Because the else part of an if-else is optional,there is an ambiguity when an else if omitted from a nested if sequence. This is resolved by associating the else with the closest previous else-less if. For example, in

因为if-else 语句的else 部分是可选的,所以在嵌套的if 语句中省略它的else 分将导致歧义。解决的方法是将每个else与最近的前一个没有else配对的if进行匹配。例如,在下列语句中:

 

if (n > 0)

if (a > b)

z = a;

else

z = b;

the else goes to the inner if, as we have shown by indentation. If that isn't what you want, braces must be used to force the proper association:

else 部分与内层的if 匹配,我们通过程序的缩进结构也可以看出来。如果这不符合我们的意图,则必须使用花括号强制实现正确的匹配关系:

 

if (n > 0) {

if (a > b)

z = a;

}

else

z = b;

The ambiguity is especially pernicious in situations like this:

if (n > 0)

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

if (s[i] > 0) {

printf("...");

return i;

}

else /* WRONG */

printf("error -- n is negative\n");

The indentation shows unequivocally what you want, but the compiler doesn't get the message, and associates the else with the inner if. This kind of bug can be hard to find; it's a good idea to use braces when there are nested ifs.

程序的缩进结构明确地表明了设计意图,但编译器无法获得这一信息,它会将else部分与内层的if配对。这种错误很难发现,因此我们建议在有if语句嵌套的情况下使用花括号。

By the way, notice that there is a semicolon after z = a in

if (a > b)

z = a;

else

z = b;

This is because grammatically, a statement follows the if, and an expression statement like ``z = a;'' is always terminated by a semicolon.

z = a 后有一个分号。这是因为,从语法上讲,跟在if后面的应该是一条语句,而像“z=a;这类的表达式语句总是以分号结束的。


3.3 Else-If

The construction

if (expression)

statement

else if (expression)

statement

else if (expression)

statement

else if (expression)

statement

else

statement

occurs so often that it is worth a brief separate discussion. This sequence of if statements is the most general way of writing a multi-way decision. The expressions are evaluated in order; if an expression is true, the statement associated with it is executed, and this terminates the whole chain. As always, the code for each statement is either a single statement, or a group of them in braces.

因此我们在这里单独说明一下。这种if语句序列是编写多路判定最常用的方法。其中的各表达式将被依次求值,一旦某个表达式结果为真,则执行与之相关的语句,并终止整个语句序列的执行。同样,其中各语句既可以是单条语句,也可以是用花括号括住的复合语句。

 

The last else part handles the ``none of the above'' or default case where none of the other conditions is satisfied. Sometimes there is no explicit action for the default; in that case the trailing

最后一个else部分用于处理“上述条件均不成立”的情况或默认情况,也就是当上面各条件都不满足时的情形。有时候并不需要针对默认情况执行显式的操作,这种情况下,可以把该结构末尾的

 

else

statement

can be omitted, or it may be used for error checking to catch an ``impossible'' condition.

部分省略掉;该部分也可以用来检查错误,以捕获“不可能”的条件。 

To illustrate a three-way decision, here is a binary search function that decides if a particular value x occurs in the sorted array v. The elements of v must be in increasing order. The function returns the position (a number between 0 and n-1) if x occurs in v, and -1 if not.

这里通过一个折半查找函数说明三路判定程序的用法。该函数用于判定已排序的数组v中是否存在某个特定的值x。数组v的元素必须以升序排列。如果v中包含x,则该函数返回xv中的位置(介于0n-1 之间的一个整数);否则,该函数返回-1 

Binary search first compares the input value x to the middle element of the array v. If x is less than the middle value, searching focuses on the lower half of the table, otherwise on the upper half. In either case, the next step is to compare x to the middle element of the selected half. This process of dividing the range in two continues until the value is found or the range is empty.

在折半查找时,首先将输入值x 与数组v 的中间元素进行比较。如果x 小于中间元素的值,则在该数组的前半部分查找;否则,在该数组的后半部分查找。在这两种情况下,下一步都是将x 与所选部分的中间元素进行比较。这个过程一直进行下去,直到找到指定的值或查找范围为空。

 

/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */

int binsearch(int x, int v[], int n)//v[0]=0,v[1]=1,v[2]=2,v[3]=3,v[4]=4,v[5]=5,

{

int low, high, mid;

low = 0;

high = n - 1;

while (low <= high) {

mid = (low+high)/2;

if (x < v[mid])

high = mid + 1;

else if (x > v[mid])

low = mid + 1;

else /* found match */

return mid;

}

return -1; /* no match */

}

The fundamental decision is whether x is less than, greater than, or equal to the middle element v[mid] at each step; this is a natural for else-if.

该函数的基本判定是:在每一步判断x 小于、大于还是等于中间元素v[mid]。使用else-if结构执行这种判定很自然。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值