说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix1/conditionals.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
16.3. Conditionals and Loops
表 1 表明,C 和 Java 中 if-else 语句的语法和语义是相同的。
Table 1 shows that the syntax and semantics of
if-elsestatements in C and Java are the same.
表 1. Java 与 C 中 if-else 语句的语法比较
Table 1. Syntax Comparison of if-else Statements in Java and C
Java 版本
Java version
1 | /* Java if-else example */ |
C 版本
C version
1 | /* C if-else example */ |
Java 和 C 中 if-else 语句的语法完全相同。在两种语言中,else 部分都是可选的。Java 和 C 也都支持通过串接 if 和 else if 语句来实现多路分支。下面是完整的 C if-else 语法:
The Java and C syntax for
if-elsestatements is identical. In both, theelsepart is optional. Java and C also support multi-way branching by chainingifandelse ifstatements. The following describes the fullif-elseC syntax:
1 | // a one-way branch: |
16.3.1. C 中的布尔值
16.3.1. Boolean Values in C
C 没有提供取值为 true 或 false 的布尔类型。取而代之的是,整数值在条件语句中会被当作真(true)或假(false)来求值。在条件表达式中,任何整数表达式:
C doesn’t provide a Boolean type with true or false values. Instead, integer values evaluate to true or false when used in conditional statements. When used in conditional expressions, any integer expression that is:
现代更新(截至 2026-11)· C 早已有布尔类型
原书说「C 没有提供布尔类型」,这只对 C89 成立。从 C99 起,C 有内置类型 _Bool,包含 <stdbool.h> 后可以写作 bool、true、false;到了 C23,bool、true、false 直接成为语言关键字,无需包含头文件。条件语句中「0 为假、非 0 为真」的规则不变。cppreference:Boolean type support · cppreference:C23
- 为零(0)时求值为假
- zero (0) evaluates to false
- 为非零(任何正值或负值)时求值为真
- nonzero (any positive or negative value) evaluates to true
C 有一组用于布尔表达式的关系运算符和逻辑运算符,它们与 Java 的关系运算符和逻辑运算符完全相同。
C has a set of relational and logical operators for Boolean expressions that are identical to Java’s relational and logical operators.
关系运算符(relational operator)的操作数类型相同,求值结果为零(假)或非零(真)。关系运算符包括:
The relational operators take operand(s) of the same type and evaluate to zero (false) or nonzero (true). The set of relational operators are:
- 相等(
==)和不等(!=)
- equality (
==) and inequality (not equal,!=)
- 比较运算符:小于(
<)、小于等于(<=)、大于(>)和大于等于(>=)
- comparison operators: less than (
<), less than or equal (<=), greater than (>), and greater than or equal (>=)
下面是几段展示关系运算符用法的 C 代码片段:
Here are some C code snippets showing examples of relational operators:
1 | // assume x and y are ints, and have been assigned |
C 的逻辑运算符(logical operator)以整数形式的「布尔」值为操作数,求值结果为零(假)或非零(真)。逻辑运算符包括:
C’s logical operators take integer “Boolean” operand(s) and evaluate to either zero (false) or nonzero (true). The set of logical operators are:
- 逻辑非(
!)
- logical negation (
!)
- 逻辑与(
&&):遇到第一个为假的表达式就停止求值(短路求值)
- logical and (
&&): stops evaluating at the first false expression (short-circuiting)
- 逻辑或(
||):遇到第一个为真的表达式就停止求值(短路求值)
- logical or (
||): stops evaluating at the first true expression (short-circuiting)
C 的逻辑运算符采用短路(short-circuit)求值:一旦逻辑表达式的结果已经确定,就停止求值。例如,如果逻辑与(&&)表达式的第一个操作数为假,那么整个 && 表达式的结果必然为假。因此第二个操作数的值无需求值,也确实不会被求值。
C’s short-circuit logical operator evaluation stops evaluating a logical expression as soon as the result is known. For example, if the first operand to a logical and (
&&) expression evaluates to false, the result of the&&expression must be false. As a result, the second operand’s value need not be evaluated, and it is not evaluated.
下面是一个在 C 条件语句中使用逻辑运算符的例子(对于复杂的布尔表达式,最好总是加上括号,使其更易读):
The following is an example of conditional statements in C that use logical operators (it’s always best to use parentheses around complex Boolean expressions to make them easier to read):
1 | if ( (x > 10) && (y >= x) ) { |
16.3.2. C 中的循环
16.3.2. Loops in C
Java 和 C 都在语言层面支持重复执行一段代码。与 Java 一样,C 支持 for、while 和 do-while 循环。这些循环在两种语言中的语法和语义完全相同。Java 还支持对集合进行迭代,而 C 没有这种功能。
Java and C both have language-level support for repeating a sequence of code. Like Java, C supports
for,while, anddo-whileloops. The syntax and semantics of these are identical in both languages. Java additionally has support for iterating over collections that C does not have.
while 循环
while Loops
C 和 Java 中的 while 循环语法完全相同,行为也一样。表 2 展示了一个带 while 循环的 C 示例程序。
The
whileloop syntax in C and Java is identical, and the behavior is the same. Table 2 shows an example C program with awhileloop.
表 2. C 中的 while 循环语法
Table 2. while Loop Syntax in C
C 的 while 循环示例
C while loop example
1 | /* C while loop example */ |
C 中的 while 循环语法与 Java 中的相同,两者的求值方式也一样:
The
whileloop syntax in C is the same as in Java and both are evaluated in the same way:
1 | while ( <boolean expression> ) { |
while 循环先检查布尔表达式,如果为真就执行循环体。在前面的示例程序中,val 变量的值会在 while 循环中被反复打印,直到它的值大于 num 变量的值为止。如果用户输入 10,C 和 Java 程序都会打印:
The
whileloop checks the Boolean expression first and executes the body if true. In the preceding example program, the value of thevalvariable will be repeatedly printed in thewhileloop until its value is greater than the value of thenumvariable. If the user enters10, the C and Java programs will print:
1 | 1 |
Java 和 C 也都有 do-while 循环,它与 while 循环类似,但先执行循环体,再检查条件,只要条件为真就重复执行循环体。也就是说,do-while 循环总会至少执行一次循环体:
Java and C also has a
do-whileloop that is similar to itswhileloop, but it executes the loop body first and then checks a condition and repeats executing the loop body for as long as the condition is true. That is, ado-whileloop will always execute the loop body at least one time:
1 | do { |
想看更多 while 循环的例子,可以试试下面两个程序:
For additional
whileloop examples, try these two programs:
for 循环
for Loops
C 中的 for 循环与 Java 中的 for 循环相同,求值方式也一样。C(和 Java)的 for 循环语法是:
The
forloop in C is the same as theforloop in Java, and they are evaluated in the same way. The C (and Java)forloop syntax is:
1 | for ( <initialization>; <boolean expression>; <step> ) { |
for 循环的求值规则是:
The
forloop evaluation rules are:
- 第一次进入循环时,对初始化部分求值一次。
- Evaluate initialization one time when first entering the loop.
- 对布尔表达式求值。如果它为 0(假),就跳出
for循环(即程序不再重复执行循环体语句)。
- Evaluate the boolean expression. If it’s 0 (false), drop out of the
forloop (that is, the program is done repeating the loop body statements).
- 执行循环体中的语句。
- Evaluate the statements inside the loop body.
- 对步进(step)表达式求值。
- Evaluate the step expression.
- 从第(2)步开始重复。
- Repeat from step (2).
下面是一个打印 0、1、2 的简单 for 循环示例:
Here’s a simple example
forloop to print the values 0, 1, and 2:
1 | int i; |
对上面的循环套用 for 循环的求值规则,会得到如下的动作序列:
Executing the
forloop evaluation rules on the preceding loop yields the following sequence of actions:
1 | (1) eval init: i is set to 0 (i=0) |
下面的程序展示了一个更复杂的 for 循环例子(也可以下载)。注意,C 虽然支持在 for 循环的初始化和步进部分写一串语句,但最好还是保持简单(这个例子展示了一种更复杂的 for 循环语法,但如果把它简化,把步进语句 j += 10 移到循环体的末尾,只保留一条步进语句 i += 1,这个 for 循环会更容易阅读和理解)。
The following program shows a more complicated
forloop example (it’s also available to download). Note that just because C supportsforloops with a list of statements for its initialization and step parts, it’s best to keep it simple (this example illustrates a more complicatedforloop syntax, but theforloop would be easier to read and understand if it were simplified by moving thej += 10step statement to the end of the loop body and having just a single step statement,i += 1).
1 | /* An example of a more complex for loop which uses multiple variables. |
与 Java 一样,在 C 中,for 循环和 while 循环的表达能力是相同的,即任何 while 循环都可以表示为 for 循环,反之亦然。
As in Java, in C
forloops andwhileloops are equivalent in power, meaning that anywhileloop can be expressed as aforloop, and vice versa.
来看下面这个 C 中的 while 循环:
Consider the following
whileloop in C:
1 | int guess = 0; |
这个循环可以翻译成 C 中等价的 for 循环:
This loop can be translated to an equivalent
forloop in C:
1 | int guess; |
由于在 C 中 for 循环和 while 循环的表达能力相同,语言中其实只需要一种循环结构。然而,for 循环更适合表达确定次数的循环(例如遍历一个范围内的值),而 while 循环更适合表达不确定次数的循环(例如一直重复,直到用户输入一个偶数为止)。因此,C(和 Java)把两者都提供给程序员。
Because
forandwhileloops are equally expressive in C, only one looping construct is needed in the language. However,forloops are a more natural language construct for definite loops (like iterating over a range of values), whereaswhileloops are a more natural language construct for indefinite loops (like repeating until the user enters an even number). As a result, C (and Java) provides both to programmers.