CodeGym /จาวาบล็อก /สุ่ม /คำสั่งในขณะที่
John Squirrels
ระดับ
San Francisco

คำสั่งในขณะที่

เผยแพร่ในกลุ่ม
โปรแกรมแรกของเราคือลำดับของคำสั่งที่ดำเนินการทีละโปรแกรม ไม่มีส้อม ซึ่งรวมถึง HelloWorld ซึ่งแสดงคำทักทาย รวมถึงการคำนวณทางคณิตศาสตร์ หลังจากโปรแกรมแรกของเรา เราได้เรียนรู้วิธีการแยกย่อย กล่าวคือ วิธีทำให้โปรแกรมดำเนินการต่างๆ ขึ้นอยู่กับเงื่อนไขเฉพาะ นี่คือรหัสสำหรับควบคุมระบบทำความร้อนและปรับอากาศส่วนกลาง:

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

    1. 1.1. คว้าแอปเปิ้ล
    2. 1.2. ปอกเปลือกแล้วหั่นเป็นชิ้น
    3. 1.3. จัดเรียงชิ้นแอปเปิ้ลในแป้งพายในกระทะ
    4. 1.4. กลับไปที่ขั้นตอนที่ 1
คำสั่ง while - 2สมมติว่าคุณมีแอปเปิ้ลสิบลูก สองมือ และมีดหนึ่งเล่ม ในชีวิตจริง คุณปอกผลไม้ทั้งโหลตามลำดับ โดยทำตามอัลกอริธึมเดียวกันสำหรับแอปเปิลแต่ละลูก แต่เราจะทำให้โปรแกรมทำซ้ำสำหรับแต่ละแอปเปิ้ลได้อย่างไร
  • เราผูกตัวเองไว้กับจำนวนแอปเปิ้ล แต่ถ้าเรามีไม่พอ คำสั่งบางคำสั่งจะถูกดำเนินการโดยไม่มี "เพย์โหลด" (และเราอาจหั่นตัวเองในขณะที่พยายามปอกแอปเปิ้ลที่ไม่มีอยู่จริง)
  • หากมีแอปเปิ้ลมากกว่าคำสั่งให้ปอก แอปเปิ้ลบางลูกก็จะถูกปล่อยให้ปอก
  • รหัสดังกล่าวอ่านยาก มีการทำซ้ำจำนวนมากและยากที่จะแก้ไข

ลูปคือคำสั่งที่อนุญาตให้ดำเนินการซ้ำๆ

ลูป 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
}
ต่อไปนี้เป็นคำอธิบายทีละขั้นตอนเกี่ยวกับสิ่งที่เกิดขึ้นเมื่อมีการเรียกใช้รหัสนี้:
  1. เราประเมินนิพจน์บูลีนที่พบในวงเล็บหลังคำสำคัญwhile
  2. หากนิพจน์บูลีนประเมินค่าเป็นจริง คำสั่งในเนื้อความของลูปจะถูกดำเนินการ หลังจากดำเนินการคำสั่งสุดท้ายในเนื้อหาลูปแล้วเราจะไปที่ขั้นตอนที่ 1
  3. หากนิพจน์บูลีนประเมินค่าเป็นเท็จ เราจะข้ามไปที่คำสั่งแรกหลังลูป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);
นี่คือคำอธิบายว่าจะเกิดอะไรขึ้นเมื่อโค้ดนี้ถูกเรียกใช้:
  1. ตัวลูปถูกดำเนินการ (ต่อจาก คีย์เวิร์ด do )
  2. เราประเมินนิพจน์บูลีนที่พบในวงเล็บหลังคำสำคัญwhile
  3. หากนิพจน์บูลีนประเมินค่าเป็นจริง เราจะไปที่ขั้นตอนที่ 1
  4. หากนิพจน์บูลีนประเมินค่าเป็นเท็จ เราจะข้ามไปที่คำสั่งแรกหลังลูปwhile
ความแตกต่างหลักสองประการจากลูปก่อนหน้าคือ: 1) ตัวลูปถูกดำเนินการอย่างน้อยหนึ่งครั้ง และ 2) นิพจน์บูลีนได้รับการประเมินหลังจากดำเนินการตัวลูป ดังนั้นการวนซ้ำแบบwhileนี้จึงเรียกว่าการวนซ้ำด้วย postcondition คราวนี้เราจะแสดงตารางพลังของตัวเลขที่ไม่เกิน 10,000:

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 ถึง 1.4:

    1. 1.1. คว้าแอปเปิ้ล
    2. 1.2. ปอกเปลือกแล้วหั่นเป็นชิ้น
    3. 1.3. จัดเรียงชิ้นแอปเปิ้ลในแป้งพายในกระทะ
    4. 1.4. กลับไปที่ขั้นตอนที่ 1
อธิบายขั้นตอนการหั่นอย่างละเอียด:
  1. จำนวนชิ้น = 0
  2. ตราบเท่าที่จำนวนของชิ้นส่วนน้อยกว่า 12 ให้ทำตามขั้นตอน 2.1 ถึง 2.3

    1. 2.1. ตัดแอปเปิ้ลอีกชิ้นออก
    2. 2.2. จำนวนชิ้น ++
    3. 2.3. กลับไปที่ขั้นตอนที่ 2
และเราจะเพิ่มสิ่งนี้ในอัลกอริทึมการทำพายของเรา:
  1. หากมีแอปเปิ้ลอยู่ในชาม ให้ทำตามขั้นตอน 1.1 ถึง 1.6:

    1. 1.1. คว้าแอปเปิ้ล
    2. 1.2. ปอกเปลือกมัน
    3. 1.3. จำนวนชิ้น = 0
    4. 1.4. ตราบเท่าที่จำนวนของชิ้นส่วนน้อยกว่า 12 ให้ทำตามขั้นตอน 1.4.1 ถึง 1.4.3
      1. 1.4.1. ตัดแอปเปิ้ลอีกชิ้นออก
      2. 1.4.2. จำนวนชิ้น ++
      3. 1.4.3. กลับไปที่ขั้นตอนที่ 1.4
    5. 1.5. จัดเรียงชิ้นแอปเปิ้ลในแป้งพายในกระทะ
    6. 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 คุณจะได้เรียนรู้ลูปประเภทต่างๆ เข้าใจความซับซ้อน และได้รับทักษะเชิงปฏิบัติในการใช้งาน
ความคิดเห็น
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION