Boolean Expressions · 布尔表达式
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| Boolean expression/ˈbuːlɪən ekˈspreʃn/ | 布尔表达式 | bù ěr biǎo dá shì |
| relational operators/rɪˈleɪʃənl ˈɒpəreɪtəz/ | 关系运算符 | guān xì yùn suàn fú |
true or false
- A Boolean expression 布尔表达式 evaluates to exactly one of two values:
trueorfalse. - Conditions in
ifstatements and loops are Boolean expressions. - Store one in a
booleanvariable:boolean passed = score >= 60; - Everything in selection and repetition rests on these
true/falseresults.
真或假
- 布尔表达式求值为恰好两个值之一:
true或false。 if语句和循环里的条件都是布尔表达式。- 把一个存进
boolean变量:boolean passed = score >= 60; - 选择和重复里的一切都依赖于这些
true/false结果。
Relational operators
- Relational operators 关系运算符 compare two values and give a Boolean:
<less than,<=at most,>greater than,>=at least.==equal to,!=not equal to.5 > 3istrue;4 == 5isfalse.
关系运算符
- 关系运算符比较两个值并给出一个布尔值:
<小于,<=至多,>大于,>=至少。==等于,!=不等于。5 > 3是true;4 == 5是false。
== versus =
==tests equality;=assigns — completely different jobs.x == 5asks "isxequal to5?" and givestrue/false.x = 5stores5inx.- Using
=where you meant==is a classic bug (and often a compile error in a condition).
== 对 =
==检验相等;=赋值——完全不同的工作。x == 5问“x是否等于5?”并给出true/false。x = 5把5存进x。- 在本该用
==的地方用=是经典错误(在条件里往往还是编译错误)。
Predicting the value
- To evaluate a Boolean expression, plug in the values and apply the operator.
- With
x = 7:x < 10→true;x == 7→true;x != 7→false. - Read the operator carefully —
<=includes equality,<does not. - Being able to predict the result is the core skill for tracing code.
预测取值
- 要求布尔表达式的值,代入数值并应用运算符。
- 当
x = 7:x < 10→true;x == 7→true;x != 7→false。 - 仔细读运算符——
<=包含相等,<不包含。 - 能预测结果是追踪代码的核心技能。
== compares; = assigns — never confuse them in a condition. if (x = 5) is wrong (it assigns, not compares); you want if (x == 5). And mind the boundary operators: x <= 10 is true when x is exactly 10, but x < 10 is false — an off-by-one that flips many conditions.
== 比较;= 赋值——在条件里绝不要弄混。if (x = 5) 是错的(它赋值,而非比较);你要的是 if (x == 5)。并留意边界运算符:当 x 恰好为 10 时 x <= 10 为 true,但 x < 10 为 false——这个差一会翻转许多条件。
With int age = 18;:
age >= 18→true(18 is at least 18).age > 18→false(18 is not greater than 18).age == 18→true;age != 20→true.
当 int age = 18;:
age >= 18→true(18 至少为 18)。age > 18→false(18 不大于 18)。age == 18→true;age != 20→true。
A Boolean expression evaluates to true or false, using relational operators < <= > >= == !=. Remember == tests equality while = assigns, and that <=/>= include the boundary but </> don't. Store a result in a boolean variable to reuse it.
布尔表达式求值为 true 或 false,使用关系运算符 < <= > >= == !=。记住 == 检验相等而 = 赋值,且 <=/>= 包含边界而 </> 不包含。把结果存进 boolean 变量以复用它。
A Boolean result · 一个布尔结果
Boolean expressions evaluate to true (1) or false (0). · 布尔表达式求值为 true (1) 或 false (0)。
Which operator tests whether two values are equal? · 哪个运算符检验两个值是否相等?
== tests equality; = assigns. · == 检验相等;= 赋值。
With int age = 18;, the expression age > 18 is true. · 当 int age = 18; 时,表达式 age > 18 为真。
18 is not greater than 18; age >= 18 would be true. · 18 不大于 18;age >= 18 才为真。
With int x = 7;, the expression x <= 7 is true. · 当 int x = 7; 时,表达式 x <= 7 为真。
<= includes equality, so 7 <= 7 is true. · <= 包含相等,所以 7 <= 7 为真。
What value does 5 != 5 evaluate to? · 5 != 5 求值为什么?
!= means 'not equal'; 5 is equal to 5, so it's false. · != 表示“不等于”;5 等于 5,所以为 false。
A variable that stores true or false has type ___ (one word). · 存储 true 或 false 的变量的类型是 ___(填英文一词 boolean)。
boolean holds true/false. · boolean 保存 true/false。