while Loops
แนะนำใหม่
7:49
การเลือกและการทำซ้ำ
นี่คือลูปสามชุด สองชุดแรกแตกต่างกันเพียงหนึ่งตัวอักษร —เครื่องหมายน้อยกว่าแทนที่จะเป็นน้อยกว่าหรือเท่ากับ ลูปแรกทำงานสิบครั้ง ลูปที่สองทำงานสิบหนึ่ง…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| while loop/waɪl luːp/ | ลูป while |
| loop control variable/luːp kənˈtrəʊl ˈveərɪəbl/ | ตัวแปรควบคุมลูป |
| infinite loop/ˈɪnfɪnət luːp/ | ลูปไม่สิ้นสุด |
Repeat while true
- A while loop 当型循环 repeats its body as long as a condition stays
true. while (i <= 5) { sum = sum + i; i++; }- Before each pass, Java checks the condition; if
false, the loop ends. - Use
whilewhen you don't know in advance how many passes you'll need.
Test-before-each-pass
- The condition is tested before the body runs each time.
- If it's
falseon the very first check, the body runs zero times. - After each pass, the condition is re-checked before the next.
- This "check, then maybe run" order is worth memorizing.

The three loop types differ in where the condition is tested
The loop control variable
- A loop control variable 循环控制变量 drives progress toward stopping.
- Initialize it before the loop, use it in the condition, and change it inside the body.
istarts at1, the condition checksi <= 5, andi++moves it forward.- Without a change that eventually breaks the condition, the loop never ends.
Infinite loops
- An infinite loop 无限循环 happens when the condition never becomes false.
- Forgetting
i++(or updating the wrong variable) traps the program forever. - The tell: the loop makes no progress toward its stopping condition.
- Always make sure each pass moves the control variable toward the exit.
A while checks its condition BEFORE each pass — so it can run zero times. If the condition is already false, the body never runs. For a counter-controlled loop, update the counter so the condition eventually turns false; forgetting the update (e.g. no i++) can cause an infinite loop.
Summing 1 to 5:
int i = 1, sum = 0;while (i <= 5) { sum = sum + i; i++; }- Passes:
i=1sum=1,i=2sum=3,i=3sum=6,i=4sum=10,i=5sum=15; theni=6fails<= 5→ stop.sumis15.
A while loop repeats its body while a condition is true, checking before each pass (so it may run zero times). In a counter-controlled loop, initialize and test the counter and update it inside the body so the loop makes progress — forgetting the update can cause an infinite loop.
Summing 1 to 5 with while
แต่ละรอบเพิ่ม i ลงใน sum, แล้ว i++ เคลื่อนที่เข้าหาจุดออก
ลูป while จะตรวจสอบเงื่อนไข...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เงื่อนไขจะถูกทดสอบก่อนแต่ละรอบการทำงาน ดังนั้นจึงสามารถทำงานได้ 0 ครั้ง
int i=1, sum=0; while (i<=5){ sum+=i; i++; } ค่าของ sum ที่สุดท้ายคืออะไร?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
1+2+3+4+5 = 15.
การลืมอัปเดต loop control variable ภายใน body มักจะทำให้เกิด...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
หากไม่มีความคืบหน้า, เงื่อนไขจะไม่กลายเป็น false
ถ้า while condition เป็น false ในการตรวจสอบครั้งแรก, body จะรัน 0 ครั้ง
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Test-before-each-pass หมายความว่า it can run 0 times
A loop that never stops because its condition never becomes false is an ___ loop (one word)
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
An infinite loop makes no progress toward stopping
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้