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结构执行这种判定很自然。

 

内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
资 源 简 介 独立分量分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的种盲信号分离方法。它是种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的个研究热点。本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA算法以及它们之间的内在联系, 详 情 说 明 独立分量分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的种盲信号分离方法。它是种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的个研究热点。 本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA算法以及它们之间的内在联系,在此基础上重点分析了种快速ICA实现算法FastICA。物质的非线性荧光谱信号可以看成是由多个相互独立的源信号组合成的混合信号,而这些独立的源信号可以看成是光谱的特征信号。为了更好的了解光谱信号的特征,本文利用独立分量分析的思想和方法,提出了利用FastICA算法提取光谱信号的特征的方案,并进行了详细的仿真实验。 此外,我们还进行了进步的研究,探索了其他可能的ICA应用领域,如音乐信号处理、图像处理以及金融数据分析等。通过在这些领域中的实验和应用,我们发现ICA在提取信号特征、降噪和信号分离等方面具有广泛的潜力和应用前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值