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

ลูป while และ do-while ใน Java: คู่มือครบวงจร

ในการเขียนโปรแกรม มักมีงานซ้ำที่ต้องทำหลายครั้ง ลูป (Loops) ใช้ทำให้งานเหล่านี้อัตโนมัติ เขียนโค้ดได้มีประสิทธิภาพมากขึ้น

ลูป while และ do...while ใน Java ใช้รันบล็อกโค้ดซ้ำตราบใดที่เงื่อนไขยังเป็นจริง คล้ายคำสั่ง if แต่ if รันครั้งเดียว ส่วน while รันซ้ำจนเงื่อนไขเป็นเท็จ

บทช่วยสอนนี้อธิบายพื้นฐาน while และ do...while พร้อมตัวอย่างโปรแกรม Java

Java While Loop

ลูป while รันบล็อกโค้ดตราบใดที่เงื่อนไขในวงเล็บเป็น true ไวยากรณ์:

while (condition_is_met) { 
    // Code to execute 
}

while ตรวจสอบเงื่อนไข ถ้าเป็น true รันโค้ดในบล็อก แล้วตรวจสอบอีกครั้ง ทำซ้ำจนเงื่อนไขเป็น false แล้วหยุดลูป รันต่อโปรแกรมหลัก

ตัวอย่าง: ร้านทำโต๊ะ

สมมติคุณช่างทำโต๊ะ ขายโต๊ะรุ่นใหม่ ผลิตได้สูงสุด 5 ตัว เกินกว่านั้นต้อง waitlist ต้องการโปรแกรมบอกว่าเหลือสั่งได้อีกกี่ตัว

int limit = 5; 
int orders_made = 0; 
while (orders_made < limit) { 
    orders_made++; 
    int capacity = limit - orders_made; 
    String message = capacity + " more tables can be ordered."; 
    System.out.println(message); 
}

ผลลัพธ์:

4 more tables can be ordered.
3 more tables can be ordered.
2 more tables can be ordered.
1 more tables can be ordered.
0 more tables can be ordered.

อธิบาย: limit = ขีดจำกัด 5 ตัว orders_made = จำนวนที่สั่งไปแล้ว ลูปทำงาน while orders_made < limit เพิ่ม orders_made ทีละ 1 คำนวณ capacity ที่เหลือ พิมพ์ข้อความ หยุดเมื่อ orders_made ไม่ต่ำกว่า limit

Java Do-While Loop

do...while เป็นลูป while แบบหนึ่ง รันโค้ดใน do ก่อน 1 รอบ แล้วค่อยตรวจสอบเงื่อนไข ถ้าเป็น true ทำซ้ำ

ความแตกต่าง: while ตรวจสอบเงื่อนไข ก่อน รันโค้ด ส่วน do...while ตรวจสอบ หลัง รันโค้ดใน do

ไวยากรณ์:

do { 
    // Code to execute 
} while (condition_is_met);

ตัวอย่าง: เกมทายตัวเลข

สร้างเกมทายเลข 1-10 ให้ผู้ใช้ทายก่อน แล้วค่อยเช็ค ถ้าผิด ให้ทายใหม่ ใช้ do-while:

import java.util.Scanner; 
class GuessingGame { 
    public static void main(String[] args) { 
        int number = 8; 
        int guess = 0; 
        Scanner input = new Scanner(System.in); 
        do { 
            System.out.print("Enter a number between 1 and 10: "); 
            guess = input.nextInt(); 
        } while (number != guess); 
        System.out.println("You're correct!"); 
    } 
}

การทำงาน: ถามทายเลข ก่อน แล้วเช็คเงื่อนไข

Enter a number between 1 and 10: 9
Enter a number between 1 and 10: 1
Enter a number between 1 and 10: 4
Enter a number between 1 and 10: 5
Enter a number between 1 and 10: 8
You're correct!

อธิบาย: Import Scanner รับอินพุต ประกาศ number (คำตอบ=8) และ guess (คำทาย) ใช้ Scanner รับค่า do-while เช็คว่า guess == number หรือยัง ผิด -> รัน do อีกครั้ง ถูก -> หยุด พิมพ์ "You're correct!"

Infinite Loops (ลูปไม่มีที่สิ้นสุด)

ลูปที่รันตลอดไป หาก while condition ไม่เป็น false จะรันตลอด จนโปรแกรมแครช จึงต้องทำให้ while หยุดที่จุดใดจุดหนึ่ง

ตัวอย่าง infinite loop:

while (true) { 
    // Code to execute 
}

แต่หยุดได้ด้วย break ตัวอย่าง: ติดตามโต๊ะในสต็อก 5 ตัว หมดสต็อก -> หยุดลูป

Boolean tables_in_stock = true; 
int orders_made = 0; 
while (tables_in_stock) { 
    orders_made++; 
    if (orders_made == 5) { 
        System.out.println("We are out of stock."); 
        break; 
    } else { 
        int tables_left = 5 - orders_made; 
        String message = "There are " + tables_left + " tables in stock."; 
        System.out.println(message); 
    } 
}

ผลลัพธ์:

There are 4 tables in stock.
There are 3 tables in stock.
There are 2 tables in stock.
There are 1 tables in stock.
We are out of stock.

อธิบาย: while รันตลอดที่ tables_in_stock เป็น true แต่ไม่มีโค้ดทำให้เป็น false จึงใช้ break เมื่อ orders_made == 5 พิมพ์หมดสต็อก แล้ว break ออกจากลูป

สรุป

while รันบล็อกโค้ดตราบใดเงื่อนไขจริง หยุดเมื่อเท็จ do...while รันโค้ด 1 รอบก่อน แล้วค่อยเช็คเงื่อนไข บทช่วยสอนนี้อธิบายทั้ง 2 แบบ พร้อม infinite loop และ break ตอนนี้คุณเขียน while และ do-while ใน Java ได้อย่างมืออาชีพแล้ว!