版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 3, Control Structures(第三章,控制结构)In this chapter all the control structures in Pike will be explained. As mentioned earlier, control structures are used to control the flow of the program execution. Note that functions that make the program pause and simple function calls are not qualified as
2、control structures.(在这一章中所有在Pike中的控制结构都将会被解释。如前面所提到的,控制结构被用来控制程序运行的流程。注意那些方法会使程序中止并用简单的函数不能访问控制结构。)3.1 Conditions(条件)Pike only has two major condition control structures. We have already seen examples of both of them in Chapter two. But for completeness they will be described again in this chapter.(
3、Pike只有两个主要条件的控制结构。我们在前两章中已经看到关于它们两个的例子。要不是之前已经很完整地描述过在这一章中它们还会再次被解释。)3.1.1 ifThe simplest one is called the if statement. It can be written anywhere where a statement is expected and it looks like this: (最简单的是被称作if的条件语句。它可以像这样被写在任何地方:)if( expression ) statement1; else statement2; if(表达式) 语句1; else 语
4、句2;Please note that there is no semicolon after the parenthesis or after the else. Step by step, if does the following: (请注意在圆括号之后或者else后没有分号。一步一步地,像下面这样:)1. First it evaluates expression.2. If the result was false go to point 5. 3. Execute statement1. 4. Jump to point 6. 5. Execute statement2. 6. D
5、one. 1. 首先计算表达式。2. 如果结果是false就到第5步。3. 执行语句1。4. 跳到第6步。5. 执行语句2。6. 完成。This is actually more or less how the interpreter executes the if statement. In short, statement1 is executed if expression is true otherwise statement2 is executed. If you are interested in having something executed if the expressi
6、on is false you can drop the whole else part like this: (执行if语句实际上可以或多或少。简单地说,如果表达式的结果是true就执行语句1,否则就执行语句2。如果你对执行if语句的结果是false比较感兴趣,你可以终止所有else部分,像这样:)if( expression )statement1;if(表达式)语句1;If on the other hand you are not interested in evaluating something if the expression is false you should use t
7、he not operator to negate the true/false value of the expression. See chapter 5 for more information about the not operator. It would look like this: (如果从另一方面来说,你并不对if的表达式结果是false感兴趣,你可以使用否定操作符来否定表达式的true/false值。看第5章有很多关于否定操作符的内容。它看上去就像这样:)if( ! expression )statement2 ;if(!表达式)语句2;Any of the stateme
8、nts here and in the rest of this chapter can also be a block of statements. A block is a list of statements, separated by semicolons and enclosed by brackets. Note that you should never put a semicolon after a block of statements. The example above would look like this; (如果从另一方面来说,你并不对if的表达式结果是false
9、感兴趣,你可以使用否定操作符来否定表达式的true/false值。看第5章有很多关于否定操作符的内容。它看上去就像这样:)if ( ! expression )statement;statement;statement;if(!表达式)语句;语句;语句;3.1.2 switchA more sophisticated condition control structure is the switch statement. A switch lets you select one of many choices depending on the value of an expression an
10、d it can look something like this: (一个更复杂的条件控制结构,就是switch语句。一个switch语句可以让你从表达式中的很多值中选择一个合适的,它看上去就像这样:)switch ( expression )case constant1:statement1;break;case constant2:statement2;break;case constant3 . constant4:statement3;break;default:statement5;switch ( 表达式 )case 常量1:语句1;break;case 常量2:语句2;brea
11、k;case 常量3 . 常量4:语句3;break;default:语句5;As you can see, a switch statement is a bit more complicated than an if statement. It is still fairly simple however. It starts by evaluating the expression it then searches all the case statements in the following block. If one is found to be equal to the valu
12、e returned by the expression, Pike will continue executing the code directly following that case statement. When a break is encountered Pike will skip the rest of the code in the switch block and continue executing after the block. Note that it is not strictly necessary to have a break before the ne
13、xt case statement. If there is no break before the next case statement Pike will simply continue executing and execute the code after that case statement as well. (正如你所见到的那样,switch语句是比if语句要有一点复杂。它仍然相当简单。它首先计算表达式然后在下面的代码块中检索case语句。如果发现有合适表达式返回值的,Pike将会立刻在这个case语句中继续执行相应的代码块。当遇到break时,Pike将会跳过在switch中
14、剩下的代码。注意在下一个case语句之前不是必须要有break的。如果在下一个case语句之前没有break,Pike同样会在执行那段case语句之后再继续执行。)One of the case statements in the above example differs in that it is a range. In this case, any value between constant3 and constant4 will cause Pike to jump to statement3. Note that the ranges are inclusive, so the v
15、alues constant3 and constant4 are also valid. (在以上的例子中与一个case语句不同的是range.这里,在常量3和常量4之间的值将会造成Pike跳到语句3中。注意range是可兼容的,因此常量3和常量4的值也是有效的。)3.2 Loops(循环)Loops are used to execute a piece of code more than once. Since this can be done in quite a few different ways there are four different loop control stru
16、ctures. They may all seem very similar, but using the right one at the right time makes the code a lot shorter and simpler.(循环被用在不止一次的执行一块代码段。因此可以有很多不同方式来执行,有四种循环控制结构。它们都很相似,但是要正确的时候使用正确的方式来使代码较短而且较简单。)3.2.1 whileWhile is the simplest of the loop control structures. It looks just like an if statemen
17、t without the else part: (while是最简单的循环控制结构。它看起来只是像一个在else外的语句块。)while ( expression )statement;while( 表达式)语句;The difference in how it works isnt that big either, the statement is executed if the expression is true. Then the expression is evaluated again, and if it is true the statement is executed ag
18、ain. Then it evaluates the expression again and so forth. Here is an example of how it could be used: (它在动作上的差异不是很大,如果表达式结果是true则执行语句。然后会再次计算表达式,并且如果它为true会再次执行语句。然后再计算表达式再执行语句等等。这是一个使用它的例子:)int e=1;while(e5)show_record(e);e=e+1;This would call show_record with the values 1, 2, 3 and 4.(这个例子是当e的值是1,
19、2,3和4的时候调用show_record方法。)3.2.2 forFor is simply an extension of while. It provides an even shorter and more compact way of writing loops. The syntax looks like this: (for是while的一个简单扩展。它规定一个比较简小的循环方式。语法看起来像这样:)for ( initializer_statement ; expression ; incrementor_expression )statement ;for(初始化语句;表达式
20、;增量表达式)语句;For does the following steps: (for按以下步骤执行:)1. Executes the the initializer_statement. The initializer statement is executed only once and is most commonly used to initialize the loop variable. 2. Evaluates expression 3. If the result was false it exits the loop and continues with the progr
21、am after the loop. 4. Executes statement. 5. Executes the increment_expression. 6. Starts over from 2. 1. 执行这个初始化语句。初始化语句只在第一次被执行并且通常被使用在循环变量中初始化。2. 计算表达式。3. 如果是false它会退出循环并且继续后面的程序。4. 执行语句。5. 执行增量表达式。6. 从第二步开始。This means that the example in the while section can be written like this:意思就是在while那章中的那
22、个例子可以像现在这样写: for(int e=1; ewrite(ATDT441-9109n); / Dial 441-9109 while(modem-gets().6 != CONNECT);This example assumes you have written something that can communicate with the modem by using the functions write and gets.(这个例子假设你通过write和gets方法写一些可以与modem交流的东西。)3.2.4 foreachForeach is unique in that i
23、t does not have an explicit test expression evaluated for each iteration in the loop. Instead, foreach executes the statement once for each element in an array. Foreach looks like this: (foreach是唯一在循环中没有明确表达式的。反而,它在数组中的每个元素中执行一次语句。像这样:)foreach ( array_expression, variable )statement ;foreach(数组表达式,变
24、量)语句;We have already seen an example of foreach in the find_song function in chapter 2. What foreach does is: (我们已经看见过foreach在第二章find_song方法中的例子了,它这是样工作的:)1. It evaluates the array_expression which must return an array. 2. If the array is empty, exit the loop. 3. It then assigns the first element fr
25、om the array to the variable. 4. Then it executes the statement. 5. If there are more elements in the array, the next one is assigned to the variable, otherwise exit the loop. 6. Go to point 4. 1. 它计算这个必须能返回一个数组的数组表达式。2. 如果数组是空的,则退出循环。3. 然后从数组中分配出第一个元素给这个变量。4. 然后它执行语句。5. 如果在这个数组中有更多元素,则每次将下一个元素分配给变量
26、,否则退出循环。6. 跳到第四步。Foreach is not really necessary, but it is faster and clearer than doing the same thing with a for loop, as shown here: (foreach不见得是必要的,但是它会比一个for循环更快并且更清晰地去做相同的事情,像下面这样:)array tmp1= array_expression;for ( tmp2 = 0; tmp2 read( );if(command=quit) break;do_command(command);3.3.2 conti
27、nueContinue does almost the same thing as break, except instead of breaking out of the loop it only breaks out of the loop body. It then continues to execute the next iteration in the loop. For a while loop, this means it jumps up to the top again. For a for loop, it jumps to the incrementor express
28、ion. For a do-while loop it jumps down to the expression at the end. To continue our example above, continue can be used like this: (continue几乎与break相同,除了代替它中止循环外,它只是中止了本次循环体。然后它会继续反复执行循环。对于一个while循环来说,就是再次跳到循环体上面。对于一个for循环来说,它会跳到增量表达式中。对于一个do-while循环来说,它往下跳到表达式那。我们举例来说明continue吧,它可以这样来使用:)while(1)s
29、tring command=Stdio.Readline()-read( );if(strlen(command) = 0) continue;if(command=quit) break;do_command(command);This way, do_command will never be called with an empty string as argument.(这种方式,用一个空字符串作为参数的话,do_command方法将从不被执行。)3.3.3 returnReturn doesnt just exit the loop, it exits the whole funct
30、ion. We have seen several examples how to use it chapter 2. None of the functions in chapter two returned anything in particular however. To do that you just put the return value right after return. Of course the type of the return value must match the type in the function declaration. If your funct
31、ion declaration is int main() the value after return must be an int. For instance, if we wanted to make a program that always returns an error code to the system, just like the UNIX command false this is how it would be done: (return不是退出循环,它是退出整个方法。我们已经在第二章节中看到过使用它的几个例子。尤其在第二章中这个方法没有返回什么。你只要把返回值放到return后面即可。当然返回值的类型必须要和声明方法时的类型相匹配。如果你的方法声明是int main(),return后面的值就必须是一个int类型的。比如,如果我们想要程序总是返回一个错误代码给系统,就像UNIX命令是false一样,像下面这样:)#!/usr/local/bin/pikeint
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- MT/T 1308-2025掘锚一体机通用技术条件
- 蔡李佛十字拳动作详细讲解
- 期中冲刺提分试题及答案
- 数学三模拟试卷(2026考研全国统考·完整版)
- 全国统考数学一历年真题|2026考研(详细解析)
- 高血压患者护理课件
- 鼻前庭炎诊疗指南
- 旧房翻新改造施工协议书 老房子局部装修改造简易版合同
- 《地球的内部》教案-2026-2027学年湘科版(新版)小学科学五年级上册
- 国际臭氧层保护日大气污染防控
- 新版部编人教版六年级上册语文全册新优教学设计(2026年秋改版教材)
- 2025-2026年网络安全法律法规与标准知识点巩固习题
- 小学三年级科学《无处不在的空气》教学设计
- 2026年秋季学期初中人教版(新教材)生物七年级上册教学计划附进度表
- 变电土建设计培训
- IPC 7711-7721-2020 中文版 电子组件返工、修改与维修标准(含返修后清洗要求)
- 钢结构工程绿色施工方案
- 新教材部编人教版五年级上册道德与法治(课件)第1课开天辟地的大事变
- 新版2026秋统编版(新版)小学道德与法治四年级上册(全册)知识点清单梳理
- 2026年部编版新教材道德与法治四年级上册全册教案设计(共4个单元含教学计划)
- TZJFPA-城市消防体检评估标准
评论
0/150
提交评论