1.3 The for statement for语句

There are plenty of different ways to write a program for a particular task. Let's try a variation on the temperature converter.

   1    #include <stdio.h>
   2 
   3    /* print Fahrenheit-Celsius table */
   4    main()
   5    {
   6        int fahr;
   7 
   8        for (fahr = 0; fahr <= 300; fahr = fahr + 20)
   9            printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
  10    }

This produces the same answers, but it certainly looks different. One major change is the elimination of most of the variables; only fahr remains, and we have made it an int. The lower and upper limits and the step size appear only as constants in the for statement, itself a new construction, and the expression that computes the Celsius temperature now appears as the third argument of printf instead of a separate assignment statement.

对于某个特定任务我们可以采用多种方法来编写程序。下面这段代码也可以实现前面的温度转换程序的功能:

   1 #include <stdio.h>
   2 
   3 /* print Fahrenheit-Celsius table */
   4 main()
   5 {
   6     int fahr;
   7 
   8     for (fahr = 0; fahr <= 300; fahr = fahr + 20)
   9         printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
  10 }

这个程序与上节中介绍的程序执行结果相同,但程序本身却有所不同。最主要的改进在于它去掉了大部分变量,而只使用了一个int类型的变量fahr。在新引入的for语句中,温度的下限、上限和步长都是常量,而计算摄氏温度的表达式现在变成了printf函数的第三个参数,它不再是一个单独的赋值语句。

This last change is an instance of a general rule - in any context where it is permissible to use the value of some type, you can use a more complicated expression of that type. Since the third argument of printf must be a floating-point value to match the %6.1f, any floating-point expression can occur here.

以上几点改进中的最后一点是C语言中一个通用规则的实例:在允许使用某种类型变量值的任何场合,都可以使用该类型的更复杂的表达式。因为printf函数的第三个参数必须是与%6.1f匹配的浮点值,所以可以在此处使用任何浮点表达式。

The for statement is a loop, a generalization of the while. If you compare it to the earlier while, its operation should be clear. Within the parentheses, there are three parts, separated by semicolons. The first part, the initialization

   fahr = 0

is done once, before the loop proper is entered. The second part is the test or condition that controls the loop:

   fahr <= 300

This condition is evaluated; if it is true, the body of the loop (here a single ptintf) is executed. Then the increment step

   fahr = fahr + 20

is executed, and the condition re-evaluated. The loop terminates if the condition has become false. As with the while, the body of the loop can be a single statement or a group of statements enclosed in braces. The initialization, condition and increment can be any expressions.

for语句是一种循环语句,它是对while语句的推广。如果将for语句与前面介绍的while语句比较,就会发现for语句的操作更直观一些。圆括号中共包含3个部分,各部分之间用分号隔开。第一部分

fahr = 0

是初始化部分,仅在进入循环前执行一次。第二部分

fahr <= 300

是控制循环的测试或条件部分。循环控制将对该条件求值,如果结果值为真(true),则执行循环体(本例中的循环体仅包含一个printf函数调用语句)。此后将执行第三部分

fahr = fahr + 20

以将循环变量fahr增加一个步长,并再次对条件求值。如果计算得到的条件值为假(false),循环将终止执行。与while语句一样,for循环语句的循环体可以只有一条语句,也可以是用花括号括起来的一组语句。初始化部分(第一部分)、条件部分(第二部分)与增加步长部分(第三部分)都可以是任何表达式。

The choice between while and for is arbitrary, based on which seems clearer. The for is usually appropriate for loops in which the initialization and increment are single statements and logically related, since it is more compact than while and it keeps the loop control statements together in one place.

在实际编程过程中,可以选择while与for中的任意一种循环语句,主要要看使用哪一种更清晰。for语句比较适合初始化和增加步长都是单条语句并且逻辑相关的情形,因为它将循环控制语句集中放在一起,且比while语句更紧凑。

Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.

练习1-5 修改温度转换程序,要求以逆序(即按照从300度到0度的顺序)打印温度转换表。

TCPL/1.03_The_for_statement (last edited 2008-02-23 15:37:03 by localhost)

ch3n2k.com | Copyright (c) 2004-2022 czk.