Booleans and selection: if/else and logic · 布尔值与选择:if/else 和逻辑
Booleans and selection
- A boolean is a value that is either
trueorfalse. - Programs make choices by testing a boolean and running different code.
- In this lesson you learn
if/else if/elseand how to build conditions.
布尔值与选择
- 布尔值(boolean)是一个值,它要么是
true,要么是false。 - 程序通过判断一个布尔值并运行不同的代码来做出选择。
- 在这节课里,你学习
if/else if/else,以及如何构造条件。
Relational operators
- These compare two values and give a
boolean:<less than,>greater than<=less or equal,>=greater or equal==equal,!=not equal
- Note:
=puts a value into a variable, but==tests if two values are equal.
关系运算符
- 这些运算符比较两个值,并给出一个
boolean:<小于,>大于<=小于或等于,>=大于或等于==等于,!=不等于
- 注意:
=把一个值放进变量里,而==是判断两个值是否相等。
public class Main {
public static void main(String[] args) {
int score = 75;
System.out.println(score > 50); // true
System.out.println(score == 100); // false
System.out.println(score != 75); // false
}
}
if / else if / else
ifruns a block only when its condition istrue.else ifchecks another condition when the ones before werefalse.elseruns when none of the conditions were true.
if / else if / else
if只有在它的条件为true时才运行那个代码块。else if在前面的条件都为false时,再检查另一个条件。else在没有任何条件为真时运行。
public class Main {
public static void main(String[] args) {
int n = -4;
if (n > 0) {
System.out.println("positive");
} else if (n < 0) {
System.out.println("negative");
} else {
System.out.println("zero");
}
}
}
Logical operators
&&means and: true only when both sides are true.||means or: true when at least one side is true.!means not: it flipstruetofalseandfalsetotrue.
逻辑运算符
&&表示并且(and):只有两边都为真时才为真。||表示或者(or):至少一边为真时就为真。!表示非(not):它把true变成false,把false变成true。
public class Main {
public static void main(String[] args) {
int age = 16;
boolean teen = age >= 13 && age <= 19;
System.out.println(teen); // true
System.out.println(!teen); // false
System.out.println(age < 13 || age > 19); // false
}
}
Short-circuit
- Java reads
&&and||from left to right and stops early when it can. - With
&&, if the left side isfalse, the answer is alreadyfalse, so the right side is skipped. - With
||, if the left side istrue, the answer is alreadytrue, so the right side is skipped. - This lets you check something is safe before you use it.
短路求值
- Java 从左到右读
&&和||,并在能确定结果时提前停止。 - 对于
&&,如果左边是false,结果就已经是false,所以右边会被跳过。 - 对于
||,如果左边是true,结果就已经是true,所以右边会被跳过。 - 这让你可以在使用某个东西之前先检查它是否安全。
public class Main {
public static void main(String[] args) {
int x = 0;
// Left side is false, so x / x is never run (no error).
boolean ok = (x != 0) && (10 / x > 1);
System.out.println(ok); // false
}
}
The == vs .equals() String trap
- For
intandboolean,==works fine. - For
String,==checks if they are the same object in memory, not the same letters. - Two Strings with the same letters can still be different objects, so
==may befalse. - Always compare Strings with
.equals(...).
== 与 .equals() 的字符串陷阱
- 对于
int和boolean,==工作得很好。 - 对于
String,==检查它们是否是内存中同一个对象,而不是相同的字母。 - 两个字母相同的字符串仍然可能是不同的对象,所以
==可能是false。 - 永远用
.equals(...)来比较字符串。
public class Main {
public static void main(String[] args) {
String a = "cat";
String b = new String("cat");
System.out.println(a == b); // false (different objects)
System.out.println(a.equals(b)); // true (same letters)
}
}
Common mistakes
- A condition must be a
boolean;if (x = 5)does not compile. - Each
switchcase needs abreak;.
常见错误
- 条件必须是
boolean;if (x = 5)无法编译。 - 每个
switch分支都要break;。
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
现在轮到你
- 每个任务都已经填好了类的骨架 —— 把你的代码写在 main 里面,或者补全给出的方法。
- 按运行来编译并运行,然后按检查答案。
- 你的代码在服务器上编译并运行,所以第一次运行也很快。
Boolean logic (and/or/not) · 布尔逻辑(and/or/not)
&&, ||, ! combine true/false values — see the truth table. · &&、||、! 组合真/假值——看真值表。
The variable n is -4. Use if / else if / else to print exactly one word: positive if n > 0, negative if n < 0, or zero otherwise. · 变量 n 是 -4。用 if / else if / else 精确地打印一个词:如果 n > 0 打印 positive,如果 n < 0 打印 negative,否则打印 zero。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete equal(String a, String b) so it returns true when the two Strings have the same letters. Use .equals, not ==. · 完成 equal(String a, String b),当两个字符串有相同的字母时返回 true。使用 .equals,不要用 ==。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete inRange(int x) so it returns true when x is between 10 and 20, including both ends (10 and 20 count). Use &&. · 完成 inRange(int x),当 x 在 10 到 20 之间(包含两端,10 和 20 都算)时返回 true。使用 &&。
Click Run to see the output here. · 点击“运行”查看此处输出。