Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

คำสั่ง If-Else ใน Java: คู่มือการใช้งานครบวงจร

เวลาเขียนโปรแกรม มักเขียนโค้ดที่ควรรันก็ต่อเมื่อเงื่อนไขบางอย่างเป็นจริง

เช่น สั่งซื้อใน e-commerce ได้ก็ต่อเมื่อ user ส่งที่อยู่แล้ว หรือร้านกาแฟ ให้ส่วนลดถ้าสั่งกาแฟ > 5 แก้วในสัปดาห์ที่แล้ว

ในโปรแกรมมิ่ง ใช้ Conditional Statements (คำสั่งเงื่อนไข) สำหรับสิ่งนี้ รันบล็อกโค้ดตามเงื่อนไข true/false ใน Java if...else เป็น control flow statement รันบล็อกโค้ดก็ต่อเมื่อเงื่อนไขเป็นจริง

บทช่วยสอนนี้สำรวจการใช้ if...else ใน Java พร้อมตัวอย่าง

Java If Statement

if statement พื้นฐานที่สุด ประเมิน statement เป็น true/false รันก็ต่อเมื่อ true ถ้า false -> ข้าม if ไปรันโค้ดต่อ

ไวยากรณ์ Java if:

if (condition_is_met) { 
    // Execute code 
}

อธิบาย: condition = boolean expression คืน true/false โค้ดใน {} รันถ้า condition true โค้ดใน if ถูก indent ไม่ต้องมี semicolon ท้าย (ต่างจากโค้ด Java ทั่วไป)

ตัวอย่าง: ส่วนลดกาแฟ

ร้านกาแฟ ให้ส่วนลด 10% ถ้าลูกค้าสั่ง > 5 แก้วในสัปดาห์ที่แล้ว

public class CheckDiscount { 
    public static void main(String[] args) { 
        int coffees_ordered_in_last_week = 4; 
        int discount = 0; 
        if (coffees_ordered_in_last_week > 5) { 
            discount = 10; 
        } 
        System.out.println("This customer is eligible for a " + discount + "% discount."); 
    } 
}

ผลลัพธ์: This customer is eligible for a 0% discount.

อธิบาย: คลาส CheckDiscount ตัวแปร coffees_ordered_in_last_week=4 discount=0 if เช็ค > 5 ไม่จริง (4 ไม่ > 5) -> ข้าม discount=10 พิมพ์ discount 0%

If-Else ใน Java

if รันโค้ดตอน condition true แต่อาจอยากรันโค้ดอื่นตอน false -> ใช้ else

else เขียนหลัง if ไม่มีเงื่อนไข optional รันก็ต่อเมื่อ if condition false

ไวยากรณ์ if-else:

if (condition_is_met) { 
    // Execute code 
} else { 
    // Execute other code 
}

ตัวอย่าง: โปรโมชันวันเสาร์

โปรโมชัน: ทุกคนได้ส่วนลด 5% วันเสาร์ (วันนี้เสาร์) แต่คนที่สั่ง > 5 แก้วได้ 10% (ดีกว่า)

class CheckDiscount { 
    public static void main(String[] args) { 
        int coffees_ordered_in_last_week = 4; 
        int discount = 0; 
        if (coffees_ordered_in_last_week > 5) { 
            discount = 10; 
        } else { 
            discount = 5; 
        } 
        System.out.println("This customer is eligible for a " + discount + "% discount."); 
    } 
}

ผลลัพธ์: This customer is eligible for a 5% discount.

เหมือนตัวอย่างแรก แต่เพิ่ม else: coffees_ordered_in_last_week > 5 เป็น false -> รัน else discount=5 พิมพ์ 5%

If-Else-If ใน Java

อยากประเมินหลายเงื่อนไข รันโค้ดตามเงื่อนไขไหนเป็นจริง -> ใช้ if...else...if เช็คเงื่อนไขแรก แล้วค่อยเช็คต่อ

ไวยากรณ์:

if (condition1_is_met) { 
    // Run condition 1 code 
} else if (condition2_is_met) { 
    // Run condition 2 code 
} else { 
    // Run other code 
}

if statements รันจากบนลงล่าง เงื่อนไขไหน true ก่อน -> รันบล็อกนั้น ไม่มีอันไหน true -> รัน else

ตัวอย่าง: ส่วนลด 3 ระดับ

เพิ่ม: สั่ง > 10 แก้ว -> ส่วนลด 15% (ลูกค้าสั่งให้เพื่อน/ที่ทำงาน อยาก retain พวกเขา)

class CheckDiscount { 
    public static void main(String[] args) { 
        int coffees_ordered_in_last_week = 4; 
        int discount = 0; 
        if (coffees_ordered_in_last_week > 5) { 
            discount = 10; 
        } else if (coffees_ordered_in_last_weel > 10) { 
            discount = 15; 
        } else { 
            discount = 5; 
        } 
        System.out.println("This customer is eligible for a " + discount + "% discount."); 
    } 
}

ผลลัพธ์: This customer is eligible for a 5% discount.

ลูกค้าสั่ง 4 แก้ว -> else รัน discount=5 ถ้าสั่ง > 5 -> discount=10 ถ้า > 10 -> discount=15 (หมายเหตุ: ตัวอย่างต้นฉบับมี typo "last_weel" แก้เป็น "last_week" แล้ว)

สรุป

  • if: รันบล็อกโค้ดถ้า condition true
  • if...else: รันโค้ดอีกบล็อกถ้า condition false
  • if...else...if: ประเมินหลายเงื่อนไข

บทช่วยสอนนี้อธิบายควบคุมโฟลว์ Java ด้วย if, if-else, if-else-if พร้อมตัวอย่าง ตอนนี้คุณใช้ conditional statements ใน Java ได้อย่างมืออาชีพแล้ว!