Biểu thức Boolean
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| Boolean expression/ˈbuːlɪən ekˈspreʃn/ | Biểu thức boolean |
| relational operators/rɪˈleɪʃənl ˈɒpəreɪtəz/ | Toán tử quan hệ |
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.
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.

The three families of operators: arithmetic, relational, and logical
== 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).
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.
== 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.
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.
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.
Kết quả Boolean
Các biểu thức Boolean đánh giá ra true (1) hoặc false (0).
Toán tử nào kiểm tra xem hai giá trị có bằng nhau không?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
== kiểm tra sự bằng nhau; = gán giá trị.
Với int age = 18;, biểu thức age > 18 là true.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
18 không lớn hơn 18; age >= 18 sẽ là true.
Với int x = 7;, biểu thức x <= 7 là true.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
<= bao gồm cả trường hợp bằng nhau, nên 7 <= 7 là true.
Giá trị của 5 != 5 là gì?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
!= có nghĩa 'không bằng'; 5 bằng 5, nên kết quả là false.
Một biến lưu trữ true hoặc false có kiểu ___ (một từ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
boolean chứa true/false.