Boolean Expressions · 布尔表达式
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| Boolean expression/ˈbuːlɪən ekˈspreʃn/ | 布尔表达式 | bù ěr biǎo dá shì |
| evaluates/ɪˈvæljuːeɪts/ | 求值 | qiú zhí |
| Relational operators/rɪˈleɪʃənl ˈɒpəreɪtəz/ | 关系运算符 | guān xì yùn suàn fú |
| logical operators/ˈlɒdʒɪkl ˈɒpəreɪtəz/ | 逻辑运算符 | luó jí yùn suàn fú |
| condition/kənˈdɪʃn/ | 条件 | tiáo jiàn |
| program flow/ˈprəʊɡræm fləʊ/ | 程序流 | chéng xù liú |
True-or-false questions
- Programs make decisions by asking true-or-false questions.
- A Boolean expression 布尔表达式 is any expression that evaluates 求值 to true or false.
- The answer is always one of exactly two values.
- These questions are how a program decides what to do next.
真或假的问题
- 程序通过问真或假的问题来做决定。
- 一个布尔表达式(Boolean expression)是任何求值(evaluates)为真或假的表达式。
- 答案总是恰好两个值中的一个。
- 这些问题是程序决定接下来做什么的方式。
A Boolean expression always evaluates to: · 一个布尔表达式总是求值为:
Exactly one of two values: true or false. · 恰好两个值之一:真或假。
The expression 5 > 3 evaluates to true. · 表达式 5 > 3 求值为真。
5 is greater than 3, so it is true. · 5 大于 3,所以为真。
Relational operators
- Relational operators 关系运算符 compare two values.
- They are: equal to, not equal to, greater than $>$, less than $<$, $\ge$, and $\le$.
- Each comparison gives true or false: $5 > 3$ is true, and $4 = 7$ is false.
- One relational operator gives one simple Boolean answer.
关系运算符
- 关系运算符(relational operators)比较两个值。
- 它们是:等于、不等于、大于 $>$、小于 $<$、$\ge$ 和 $\le$。
- 每个比较给出真或假:$5 > 3$ 是真,$4 = 7$ 是假。
- 一个关系运算符给出一个简单的布尔答案。
True or false? · 真还是假?
Relational operators compare values; logical operators combine them. AND needs both true; OR needs at least one; NOT reverses. · 关系运算符比较值;逻辑运算符组合它们。AND 需要两者都真;OR 需要至少一个;NOT 反转。
true AND false evaluates to: · true AND false 求值为:
AND is true only when both parts are true. · AND 只在两部分都真时为真。
true OR false evaluates to: · true OR false 求值为:
OR is true when at least one part is true. · OR 在至少一部分为真时为真。
The logical operator that reverses a value (NOT true is false) is . · 反转一个值的逻辑运算符(NOT 真是假)是。
NOT flips true to false and false to true. · NOT 把真翻成假,假翻成真。
Logical operators
- Combine conditions with logical operators 逻辑运算符:
- AND is true only when both parts are true.
- OR is true when at least one part is true.
- NOT reverses a value: NOT true is false.
逻辑运算符
- 用逻辑运算符(logical operators)组合条件:
- AND 只在两部分都为真时为真。
- OR 在至少一部分为真时为真。
- NOT 反转一个值:NOT 真是假。
Pass needs score ≥ 50 AND attendance > 80. With score 60, attendance 75, the student: · 及格需要分数 ≥ 50 AND 出勤 > 80。分数 60、出勤 75,学生:
60 ≥ 50 is true, 75 > 80 is false; true AND false is false. · 60 ≥ 50 为真,75 > 80 为假;真 AND 假是假。
Conditions control the flow
- A Boolean expression is often used as a condition 条件 that controls program flow 程序流.
- Evaluate each part in turn, then combine, to predict the result.
Pass rule. A student passes if score $\ge 50$ AND attendance $> 80$. With score 60, attendance 75: first part $60 \ge 50$ is true; second part $75 > 80$ is false; true AND false is false — no pass. If attendance were 90, both are true, so the whole is true.
条件控制流程
- 一个布尔表达式常被用作控制程序流(program flow)的条件(condition)。
- 依次对每部分求值,再组合,来预测结果。
及格规则。 一个学生及格,如果分数 $\ge 50$ AND 出勤 $> 80$。分数 60、出勤 75:第一部分 $60 \ge 50$ 为真;第二部分 $75 > 80$ 为假;真 AND 假是假——不及格。如果出勤是 90,两者都真,所以整体为真。
A Boolean expression evaluates to true or false. Relational operators ($>$, $<$, $=$, $\ne$, $\ge$, $\le$) compare two values; logical operators combine conditions — AND (both), OR (at least one), NOT (reverse). Used as a condition, a Boolean expression controls program flow.
一个布尔表达式****求值为真或假。关系运算符($>$、$<$、$=$、$\ne$、$\ge$、$\le$)比较两个值;逻辑运算符组合条件——AND(两者)、OR(至少一个)、NOT(反转)。用作条件,布尔表达式控制程序流。