Iteration: FOR · การทำซ้ำ: FOR
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
FOR counts
- A count-controlled loop runs once for each integer from the start to the end, inclusive.
- The counter is an
INTEGER. - It is good practice to repeat the counter's name after
NEXT.
ไทย
FOR นับ
- Loop แบบนับจะรันหนึ่งครั้งสำหรับแต่ละจำนวนเต็มตั้งแต่เริ่มต้นจนถึงสิ้นสุด, รวมถึงขอบเขต
- Counter เป็น
INTEGER - การปฏิบัติที่ดีคือทำซ้ำชื่อ counter หลัง
NEXT
DECLARE Total : INTEGER
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
Explore · สำรวจ
Trace the running total · ติดตามผลรวม
A FOR loop repeats a set number of times. Step through and watch Total grow. · ลูป FOR ทำซ้ำจำนวนครั้งที่กำหนด ผ่านและดู Total ขยาย
English
STEP changes the jump
STEPsets the gap between values. It can be negative.- If the start is already past the end in the step's direction, the body does not run.
ไทย
STEP เปลี่ยนระยะห่าง
STEPตั้งค่าช่องว่างระหว่างค่า มันสามารถเป็นลบได้- หากเริ่มต้นเกินสิ้นสุดไปแล้วในทิศทางของ step, body จะไม่รัน
FOR I ← 5 TO 1 STEP -2
OUTPUT I
NEXT I
English
Watch out
NEXT Imust name the counter of this loop.- The body is indented.
NEXTlines up withFOR.
ไทย
ระวัง
NEXT Iต้องระบุชื่อ counter ของ loop นี้- Body ถูกจัด indent
NEXTเรียงตรงกับFOR
English
Common mistakes
1 TO 5includes both 1 and 5.- Do not change the counter yourself inside the body.
ไทย
ข้อผิดพลาดที่พบบ่อย
1 TO 5รวมทั้ง 1 และ 5- อย่าเปลี่ยน counter ด้วยตนเองภายใน body
English
Now you try
- Use a
FORloop to print a run of numbers.
ไทย
ลองดูเลย
- ใช้
FORloop เพื่อพิมพ์ชุดตัวเลข
Use a FOR loop to output the numbers 1 to 5, one per line. · ใช้ลูป FOR เพื่อแสดงตัวเลข 1 ถึง 5, อย่างละบรรทัด
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Output 5, 3 and 1, using FOR with STEP -2. · เอาต์พุต 5, 3 และ 1, ใช้ FOR กับ STEP -2
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่