Booleans and selection: if/else and logic · Booleans และการเลือก: 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เท่านั้น - โปรแกรมเลือกทางโดยทดสอบค่า boolean และรันโค้ดที่แตกต่างกัน
- ในบทเรียนนี้คุณจะได้เรียนรู้
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.
Relational operators
- ตัวเหล่านี้เปรียบเทียบสองค่าและให้ผลลัพธ์เป็น
boolean:<น้อยกว่า,>มากกว่า<=น้อยกว่าหรือเท่ากับ,>=มากกว่าหรือเท่ากับ==เท่ากับ,!=ไม่เท่ากับ
- หมายเหตุ:
=赋值variable,但==tests if two values are equal.
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รัน block เฉพาะเมื่อเงื่อนไขเป็นtrueelse ifตรวจสอบเงื่อนไขอื่นเมื่อเงื่อนไขก่อนหน้าเป็นfalseelseรันเมื่อ ไม่มี เงื่อนไขใดเป็นจริงเลย
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.
Logical operators
&&หมายถึง and: เป็น true ก็ต่อเมื่อ ทั้งสอง ด้านเป็น true||หมายถึง or: เป็น true เมื่อ อย่างน้อยหนึ่ง ด้านเป็น true!หมายถึง 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.
Short-circuit
- 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(...).
The == vs .equals() String trap
- สำหรับ
intและboolean,==ทำงานได้ดี - สำหรับ
String,==ตรวจสอบว่า它们是相同的对象在内存中, not the same letters. - Two Strings with the same letters can still be different objects, so
==may befalse. - Always compare Strings with
.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;.
ข้อผิดพลาดที่พบบ่อย
- A condition must be a
boolean;if (x = 5)does not compile. - Each
switchcase needs abreak;.
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.
ลองดูเลย
- Each task เติม skeleton class ไว้ล่วงหน้า — เขียนโค้ดของคุณ ภายใน main, หรือเติมเต็ม method ที่แสดง
- กด Run เพื่อ compile และ run, แล้วกด Check answer
- โค้ดของคุณ compile และ run บน server, ดังนั้นแม้การ run ครั้งแรกก็เร็ว
Boolean logic (and/or/not)
&&, ||, ! combine true/false values — see the truth table. · &&, ||, ! รวมค่า true/false — ดู 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 เพื่อพิมพ์คำหนึ่งคำเท่านั้น: positive ถ้า n > 0, negative ถ้า n < 0, หรือ zero ในกรณีอื่น
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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) เพื่อให้ return true เมื่อสอง Strings มีตัวอักษรเหมือนกัน. ใช้ .equals, ไม่ใช่ ==
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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) เพื่อให้ return true เมื่อ x อยู่ระหว่าง 10 ถึง 20, รวม ทั้งสองปลาย (10 และ 20 นับด้วย). ใช้ &&
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่