Booleans และการเลือก: if/else และตรรกะ
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
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.
English
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.
English
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
}
}
English
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.
English
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");
}
}
}
English
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.
English
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
}
}
English
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.
English
public class Main {
public static void main(String[] args) {
int x = 0;
// Left side is false, so 10 / x is never run (no error).
boolean ok = (x != 0) && (10 / x > 1);
System.out.println(ok); // false
}
}
English
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(...).
English
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)
}
}
English
Common mistakes
- A condition must be a
boolean;if (x = 5)does not compile. - Compare
Strings with.equals(...), not==.==asks whether they are the same object.
English
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- For a complete program, Run compiles and runs it. For a method or class task, Check answer compiles your code with the supplied tests and shows the results.
- Your code compiles and runs on the server, so even the first run is fast.
สำรวจ
Boolean logic (and/or/not)
&&, ||, ! รวมค่า true/false — ดู truth table
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวแปร n มีค่า -4. ใช้ if / else if / else เพื่อพิมพ์คำหนึ่งคำเท่านั้น: positive ถ้า n > 0, negative ถ้า n < 0, หรือ zero ในกรณีอื่น
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เติมเต็ม equal(String a, String b) เพื่อให้ return true เมื่อสอง Strings มีตัวอักษรเหมือนกัน. ใช้ .equals, ไม่ใช่ ==
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เติมเต็ม inRange(int x) เพื่อให้ return true เมื่อ x อยู่ระหว่าง 10 ถึง 20, รวม ทั้งสองปลาย (10 และ 20 นับด้วย). ใช้ &&
คลิก Run เพื่อดูผลลัพธ์ที่นี่