โปรแกรมแรกของเราคือลำดับของคำสั่งที่ดำเนินการทีละโปรแกรม ไม่มีส้อม ซึ่งรวมถึง HelloWorld ซึ่งแสดงคำทักทาย รวมถึงการคำนวณทางคณิตศาสตร์ หลังจากโปรแกรมแรกของเรา เราได้เรียนรู้วิธีการแยกย่อย กล่าวคือ วิธีทำให้โปรแกรมดำเนินการต่างๆ ขึ้นอยู่กับเงื่อนไขเฉพาะ นี่คือรหัสสำหรับควบคุมระบบทำความร้อนและปรับอากาศส่วนกลาง:
สมมติว่าคุณมีแอปเปิ้ลสิบลูก สองมือ และมีดหนึ่งเล่ม ในชีวิตจริง คุณปอกผลไม้ทั้งโหลตามลำดับ โดยทำตามอัลกอริธึมเดียวกันสำหรับแอปเปิลแต่ละลูก แต่เราจะทำให้โปรแกรมทำซ้ำสำหรับแต่ละแอปเปิ้ลได้อย่างไร
ดำเนินการต่อ.
if (tempRoom>tempComfort)
airConditionerOn();
if (tempRoom<tempComfort)
heaterOn();
ทำตามขั้นตอนต่อไป ในชีวิตประจำวัน เรามักจะทำสิ่งซ้ำๆ เดิมๆ เช่น ปอกแอปเปิ้ลเพื่อซื้อพาย กระบวนการที่น่าสนใจนี้สามารถอธิบายได้ดังนี้:
-
หากมีแอปเปิ้ลอยู่ในชาม เราจะทำตามขั้นตอน 1.1 ถึง 1.4:
- 1.1. คว้าแอปเปิ้ล
- 1.2. ปอกเปลือกแล้วหั่นเป็นชิ้น
- 1.3. จัดเรียงชิ้นแอปเปิ้ลในแป้งพายในกระทะ
- 1.4. กลับไปที่ขั้นตอนที่ 1

- เราผูกตัวเองไว้กับจำนวนแอปเปิ้ล แต่ถ้าเรามีไม่พอ คำสั่งบางคำสั่งจะถูกดำเนินการโดยไม่มี "เพย์โหลด" (และเราอาจหั่นตัวเองในขณะที่พยายามปอกแอปเปิ้ลที่ไม่มีอยู่จริง)
- หากมีแอปเปิ้ลมากกว่าคำสั่งให้ปอก แอปเปิ้ลบางลูกก็จะถูกปล่อยให้ปอก
- รหัสดังกล่าวอ่านยาก มีการทำซ้ำจำนวนมากและยากที่จะแก้ไข
ลูปคือคำสั่งที่อนุญาตให้ดำเนินการซ้ำๆ
ลูป whileของ Java จะทำงานได้ดีในกรณีของเรา โครงสร้างนี้ทำให้การดำเนินการหลายอย่างเป็นโครงสร้างที่กระชับและเข้าใจได้ การใช้ ลูป whileอัลกอริทึมการแบ่งส่วนของแอปเปิ้ลสำหรับพายอาจมีลักษณะเช่นนี้ใน Java:
while (numberOfApplesInBowl > 0) {
apple = bowl.grabNextApple();
arrangeInPie(apple.peel().slice());
numberOfApplesInBow--; // "--" is the decrement operator, which reduces the number of apples by one
}
System.out.println("The apples for the pie have been processed.");
ไวยากรณ์คำสั่ง
รูปแบบแรกของ คำสั่ง whileเป็นดังนี้:
while (Boolean expression) {
// Loop body — the statement(s) that are repeatedly executed
}
ต่อไปนี้เป็นคำอธิบายทีละขั้นตอนเกี่ยวกับสิ่งที่เกิดขึ้นเมื่อมีการเรียกใช้รหัสนี้:
- เราประเมินนิพจน์บูลีนที่พบในวงเล็บหลังคำสำคัญwhile
- หากนิพจน์บูลีนประเมินค่าเป็นจริง คำสั่งในเนื้อความของลูปจะถูกดำเนินการ หลังจากดำเนินการคำสั่งสุดท้ายในเนื้อหาลูปแล้วเราจะไปที่ขั้นตอนที่ 1
- หากนิพจน์บูลีนประเมินค่าเป็นเท็จ เราจะข้ามไปที่คำสั่งแรกหลังลูปwhile
วนซ้ำด้วยเงื่อนไขเบื้องต้น
เนื่องจากเรามักจะประเมินนิพจน์บูลีน (เงื่อนไขสำหรับการเข้าสู่ลูป) ก่อนที่เราจะดำเนินการเนื้อความของลูป รูปแบบของลูป while นี้จึงมักเรียกว่าลูปที่มีเงื่อนไขล่วงหน้า มาสร้างตารางของเลขยกกำลังสิบตัวแรกกัน:
public static void main(String[] args) {
int base = 3; // The number that will be exponentiated
int result = 1; // The result of exponentiation
int exponent = 1; // The initial exponent
while (exponent <= 10) { // The condition for entering the loop
result = result * base;
System.out.println(base + " raised to the power of " + exponent + " = " + result);
exponent++;
}
}
เอาต์พุตคอนโซล:
3 raised to the power of 1 = 3
3 raised to the power of 2 = 9
3 raised to the power of 3 = 27
3 raised to the power of 4 = 81
3 raised to the power of 5 = 243
3 raised to the power of 6 = 729
3 raised to the power of 7 = 2187
3 raised to the power of 8 = 6561
3 raised to the power of 9 = 19683
3 raised to the power of 10 = 59049
Process finished with exit code 0
วนซ้ำด้วย postcondition
นี่คือตัวแปรที่สองของลูปนี้:
do {
// Loop body — the statement(s) that are repeatedly executed
} while (Boolean expression);
นี่คือคำอธิบายว่าจะเกิดอะไรขึ้นเมื่อโค้ดนี้ถูกเรียกใช้:
- ตัวลูปถูกดำเนินการ (ต่อจาก คีย์เวิร์ด do )
- เราประเมินนิพจน์บูลีนที่พบในวงเล็บหลังคำสำคัญwhile
- หากนิพจน์บูลีนประเมินค่าเป็นจริง เราจะไปที่ขั้นตอนที่ 1
- หากนิพจน์บูลีนประเมินค่าเป็นเท็จ เราจะข้ามไปที่คำสั่งแรกหลังลูปwhile
public static void main(String[] args) {
int base = 3; // The number that will be exponentiated
int result = base; // The result of exponentiation
int exponent = 1; // The initial exponent
do {
System.out.println(base + " raised to the power of " + exponent + " = " + result);
exponent++;
result = result * base;
} while (result < 10000); // The condition for exiting the loop
}
เอาต์พุตคอนโซล:
3 raised to the power of 1 = 3
3 raised to the power of 2 = 9
3 raised to the power of 3 = 27
3 raised to the power of 4 = 81
3 raised to the power of 5 = 243
3 raised to the power of 6 = 729
3 raised to the power of 7 = 2187
3 raised to the power of 8 = 6561
Process finished with exit code 0
ให้ความสนใจกับการเปลี่ยนแปลงในรหัส เปรียบเทียบสิ่งนี้กับลูปที่มีเงื่อนไขล่วงหน้า
ข้อเท็จจริงที่น่าสนใจเกี่ยวกับการทำงานกับลูป
แยกคำสั่งภายในตัวลูป
มีสองคำสั่งที่ส่งผลต่อการดำเนินการภายในลูป: ทำลาย (ซึ่งเราจะกล่าวถึงในรายละเอียดมากขึ้นในบทถัดไป) และ- ดำเนินการต่อ — ข้ามการดำเนินการส่วนที่เหลือของเนื้อหาลูปในการวนซ้ำปัจจุบัน และข้ามไปที่การประเมินนิพจน์บูลีนของคำสั่ง while หากนิพจน์ประเมินค่าเป็นจริง การวนซ้ำจะดำเนินต่อไป
- หยุด — ยุติการดำเนินการวนซ้ำปัจจุบันทันทีและโอนการควบคุมไปยังคำสั่งแรกหลังการวนซ้ำ ดังนั้นคำสั่งนี้จะสิ้นสุดการดำเนินการของลูปปัจจุบัน เราจะพิจารณารายละเอียดเพิ่มเติมในบทความถัดไป
while (numberOfApplesInBowl > 0) {
apple = bowl.grabNextApple();
numberOfApplesInBow--; // "--" is the decrement operator, which reduces the number of apples by one
if (apple.isBad()) { // This method returns true for rotten apples
apple.throwInGarbage();
continue; // Continue the loop. Jump to evaluation of numberOfApplesInBowl > 0
}
arrangeInPie(apple.peel().slice());
}
คำ สั่ง
ดำเนินการต่อมักใช้เมื่อคำสั่งในเนื้อหาลูปจำเป็นต้องดำเนินการหากตรงตามเงื่อนไขที่กำหนด ตัวอย่างเช่น เราอาจต้องการดำเนินการเมื่อเซ็นเซอร์ฮาร์ดแวร์ถูกกระตุ้น (มิฉะนั้น ให้ดำเนินการวนซ้ำที่เราอ่านค่าเซ็นเซอร์ต่อไป) หรือเราอาจต้องการคำนวณนิพจน์เฉพาะในการวนซ้ำบางรอบ ตัวอย่างกรณีหลังสามารถเห็นได้จากการใช้ลูป while เพื่อคำนวณผลรวมของลูกบาศก์ของจำนวนธรรมชาติที่มีกำลังสองน้อยกว่าจำนวนตัวเลข สับสน? ตรวจสอบรหัสต่อไปนี้:
public static void main(String[] args) {
int sum = 0; // Total amount
int i = 0; // Initial number in the series
int count = 20; // Number of numbers
while (i <= count) {
i++; // Get the next number — "i++" is equivalent to "i = i + 1"
if (i * i <= count) // If the square of the number is less than
continue; // the number of numbers, then we won't calculate the sum
// Jump to the next number in the loop
sum += i * i * i; // Otherwise, we calculate the sum of the cubes of numbers
} // "sum += i * i * i" is notation that is equivalent to "sum = sum + i * i * i"
System.out.println(sum); // Print the result
}
วนไม่มีสิ้นสุด
ข้อความแยกย่อยเหล่านี้ใช้บ่อยที่สุดในลูปไม่สิ้นสุด เราเรียกการวนซ้ำว่าไม่มีที่สิ้นสุดหากเงื่อนไขบูลีนสำหรับการออกจากลูปไม่เป็นที่พอใจ ในโค้ดจะมีลักษณะดังนี้:
while (true) {
// Loop body
}
ในกรณีนี้ คำสั่ง
breakช่วยให้เราออกจากลูป การวนซ้ำประเภทนี้เหมาะเมื่อรอเงื่อนไขภายนอกที่กำหนดนอกตัวลูป ตัวอย่างเช่น ในระบบปฏิบัติการหรือเกม (การออกจากลูปหมายถึงการออกจากเกม) หรือเมื่อใช้อัลกอริทึมที่พยายามวนซ้ำแต่ละครั้งเพื่อปรับปรุงผลลัพธ์บางอย่าง แต่จำกัดจำนวนการวนซ้ำตามเวลาที่ผ่านไปหรือการเกิดเหตุการณ์ภายนอก (เช่น หมากฮอส หมากรุก หรือการพยากรณ์อากาศ) โปรดจำไว้ว่าภายใต้สภาวะปกติ ไม่เป็นที่ต้องการของการวนซ้ำไม่สิ้นสุด เพื่อสาธิต ให้กลับไปที่การยกกำลัง:
public static void main(String[] args) {
int base = 3; // The number that will be exponentiated
int result = 1; // The result of exponentiation
int exponent = 1; // The initial exponent
while (true) {
result = result * base;
System.out.println(base + " raised to the power of " + exponent + " = " + result);
exponent++;
if (exponent > 10)
break; // Exit the loop
}
}
เอาต์พุตคอนโซล:
3 raised to the power of 1 = 3
3 raised to the power of 2 = 9
3 raised to the power of 3 = 27
3 raised to the power of 4 = 81
3 raised to the power of 5 = 243
3 raised to the power of 6 = 729
3 raised to the power of 7 = 2187
3 raised to the power of 8 = 6561
3 raised to the power of 9 = 19683
3 raised to the power of 10 = 59049
Process finished with exit code 0
ลูปที่ซ้อนกัน
และตอนนี้เรามาถึงหัวข้อสุดท้ายของเราเกี่ยวกับลูป นึกถึงพายแอปเปิ้ลนั่น (ฉันหวังว่าคุณจะไม่หิวในตอนนี้) และการปอกแอปเปิ้ลของเรา:-
หากมีแอปเปิ้ลอยู่ในชาม เราจะทำตามขั้นตอน 1.1 ถึง 1.4:
- 1.1. คว้าแอปเปิ้ล
- 1.2. ปอกเปลือกแล้วหั่นเป็นชิ้น
- 1.3. จัดเรียงชิ้นแอปเปิ้ลในแป้งพายในกระทะ
- 1.4. กลับไปที่ขั้นตอนที่ 1
- จำนวนชิ้น = 0
-
ตราบเท่าที่จำนวนของชิ้นส่วนน้อยกว่า 12 ให้ทำตามขั้นตอน 2.1 ถึง 2.3
- 2.1. ตัดแอปเปิ้ลอีกชิ้นออก
- 2.2. จำนวนชิ้น ++
- 2.3. กลับไปที่ขั้นตอนที่ 2
-
หากมีแอปเปิ้ลอยู่ในชาม ให้ทำตามขั้นตอน 1.1 ถึง 1.6:
- 1.1. คว้าแอปเปิ้ล
- 1.2. ปอกเปลือกมัน
- 1.3. จำนวนชิ้น = 0
- 1.4. ตราบเท่าที่จำนวนของชิ้นส่วนน้อยกว่า 12 ให้ทำตามขั้นตอน 1.4.1 ถึง 1.4.3
- 1.4.1. ตัดแอปเปิ้ลอีกชิ้นออก
- 1.4.2. จำนวนชิ้น ++ 1.4.3. กลับไปที่ขั้นตอนที่ 1.4
- 1.5. จัดเรียงชิ้นแอปเปิ้ลในแป้งพายในกระทะ
- 1.6. กลับไปที่ขั้นตอนที่ 1
public static void main(String[] args) {
// Print the second factors in a row
System.out.println(" 2 3 4 5 6 7 8 9");
int i = 2; // Assign the first factor to the variable
while (i < 10) { // First loop: execute as long as the first factor is less than 10
System.out.print(i + " | "); // Print the first factor at the beginning of the line
int j = 2; // The starting value of the second factor
while (j < 10) { // Second loop: execute as long as the second factor is less than 10
int product = i * j; // Calculate the product of the factors
if (product < 10) // If the product is a single digit, then we print two spaces after the product
System.out.print(product + " ");
else // Otherwise, print the product and one space after it
System.out.print(product + " ");
j++; // Increment the second factor by one
} // Go to the beginning of the second loop, i.e. "while (j < 10)"
System.out.println(); // Move to the next line on the console
i++; // Increment the first factor by one
} // Go to the beginning of the first loop, i.e. "while (i < 10)"
}
เอาต์พุตคอนโซล:
2 3 4 5 6 7 8 9
2 | 4 6 8 10 12 14 16 18
3 | 6 9 12 15 18 21 24 27
4 | 8 12 16 20 24 28 32 36
5 | 10 15 20 25 30 35 40 45
6 | 12 18 24 30 36 42 48 54
7 | 14 21 28 35 42 49 56 63
8 | 16 24 32 40 48 56 64 72
9 | 18 27 36 45 54 63 72 81
Process finished with exit code 0
ลูป (โดยเฉพาะอย่างยิ่ง คำสั่ง
while ) เป็นหนึ่งในองค์ประกอบพื้นฐานของซอฟต์แวร์ ด้วยการแก้ปัญหาบน CodeGym คุณจะได้เรียนรู้ลูปประเภทต่างๆ เข้าใจความซับซ้อน และได้รับทักษะเชิงปฏิบัติในการใช้งาน
GO TO FULL VERSION