ลูป for
แนะนำใหม่
7:49
การเลือกและการทำซ้ำ
นี่คือลูปสามชุด สองชุดแรกแตกต่างกันเพียงหนึ่งตัวอักษร —เครื่องหมายน้อยกว่าแทนที่จะเป็นน้อยกว่าหรือเท่ากับ ลูปแรกทำงานสิบครั้ง ลูปที่สองทำงานสิบหนึ่ง…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| for loop/fɔː luːp/ | ลูปสำหรับ |
A loop with a counter built in
- A for loop 计数循环 packs three parts into one line:
for (int i = 0; i < 5; i++) { ... }- Initialization (
int i = 0), condition (i < 5), update (i++). - Use
forwhen you know how many times to repeat.
The three parts, in order
- Initialization runs once, before the loop starts.
- The condition is checked before each pass (like
while). - The update runs after each pass, then the condition is re-checked.
- So the flow is: init → (test → body → update) → (test → body → update) → …

The three parts of a for loop and the order they run
for and while are interchangeable
- Any
forloop can be rewritten as awhileloop, and vice versa. - If
Bhas nocontinuetargeting this loop,for (int i=0; i<5; i++) {B}can be rewritten as{ int i=0; while (i<5) {B; i++;} }. foris tidier when the counter setup, test, and change belong together.- Choose whichever makes the loop's intent clearest.
Counting a known number of times
for (int i = 0; i < n; i++)runs the body exactlyntimes (i=0ton-1).- Change the bounds to count differently:
i = 1; i <= nrunsntimes too. - Watch the boundary:
<stops one before,<=includes the end. - This is the standard pattern for "do something
ntimes."
Mind the loop bounds — an off-by-one changes the count. for (int i = 0; i < n; i++) runs n times (i from 0 to n-1); switching to i <= n runs n+1 times and often overruns an array. Trace the first and last value of i to confirm the count before trusting a for loop.
Printing 0,1,2,3,4:
for (int i = 0; i < 5; i++) { System.out.println(i); }istarts0, runs whilei < 5,i++each pass.- Prints
0 1 2 3 4(five values); ati = 5the condition fails → stop.
A for loop combines initialization (once), a condition (checked before each pass), and an update (after each pass): for (int i = 0; i < n; i++) runs the body n times. It's interchangeable with a while loop. Mind the bounds — < vs <= changes the count by one.
ลูป for นับจาก 0 ถึง 4
i ทำงานตั้งแต่ 0..4 (5 รอบ); เมื่อ i=5 เงื่อนไขจะไม่เป็นจริง
ส่วนประกอบสามส่วนของหัวลูป for มีดังนี้ตามลำดับ...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
for (init; condition; update).
ร่างงานของ for (int i = 0; i < 5; i++) จะทำงานกี่ครั้ง?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
i รับค่า 0,1,2,3,4 → 5 รอบ
ส่วนการเริ่มต้นของลูป for จะทำงาน...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Init ทำงานหนึ่งครั้ง; เงื่อนไขถูกตรวจสอบทุกครั้งที่วน; Update ทำงานหลังแต่ละรอบ
ลูป for ใดๆ สามารถเขียนใหม่ให้เทียบเท่ากับลูป while ได้
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
for และ while ใช้แทนกันได้
for (int i = 1; i <= 5; i++) จะทำงานกี่ครั้ง?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
i รับค่า 1..5 (รวม) → จะผ่าน 5 ครั้ง
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้