Loops: while, for, and accumulation · Loops: while, for, และการสะสม
Repeating work
- A loop runs the same block of code many times, so you don't copy it by hand.
- C has three loops:
while,for, anddo-while. They all repeat while a condition is true. - The condition is checked each time around. When it becomes false, the loop stops.
การทำงานซ้ำๆ
- ลูป จะรันบล็อกโค้ดเดียวกันหลายครั้ง ดังนั้นคุณไม่จำเป็นต้องคัดลอกมันเอง
- C มีลูปสามแบบ:
while,forและdo-whileทั้งสามจะทำซ้ำจนกว่า เงื่อนไข จะเป็นจริง - เงื่อนไขจะถูกตรวจสอบทุกครั้งเมื่อวนรอบ เมื่อเป็นเท็จ ลูปจะหยุดลง
while loops
while (test) { ... }checks the test first, then runs the block if it is true.- Something inside the block must eventually make the test false, or the loop never ends.
- Use
whilewhen you don't know in advance how many times you will repeat.
ลูป while
while (test) { ... }ตรวจสอบการทดสอบก่อน แล้วจึงรันบล็อกหากเป็นจริง- ต้องมีสิ่งที่ภายในบล็อกทำให้การทดสอบเป็นเท็จในที่สุด มิฉะนั้นลูปจะไม่สิ้นสุด
- ใช้
whileเมื่อคุณไม่รู้ล่วงหน้าว่าจะทำซ้ำกี่ครั้ง
for loops
- A
forloop puts three parts in one line:for (start; test; step). for (int i = 0; i < 5; i++)means: startiat0, keep going whilei < 5, add1each time.- Use
forwhen you are counting a known number of times.
ลูป for
- ลูป
forวางสามส่วนไว้ในบรรทัดเดียว:for (start; test; step) for (int i = 0; i < 5; i++)หมายความว่า: เริ่มiที่0, ทำต่อไปจนกว่าi < 5, เพิ่ม1ทุกครั้ง- ใช้
forเมื่อคุณกำลังนับจำนวนครั้งที่ทราบแล้ว
#include <stdio.h>
int main(void) {
int total = 0;
for (int i = 1; i <= 4; i++) {
total = total + i; // accumulate: 1, then 3, then 6, then 10
}
printf("%d\n", total); // 10
return 0;
}
Accumulation and counting
- An accumulator is a variable that builds up a result across the loop, like
totalabove. - A counter adds
1each time something happens, to count matches. break;stops the loop early;continue;skips to the next round.
การสะสมและการนับ
- ตัวสะสม คือตัวแปรที่สร้างผลลัพธ์ขึ้นตลอดการวนลูป เช่น
totalด้านบน - ตัวนับ จะเพิ่ม
1ทุกครั้งที่เกิดเหตุการณ์ขึ้น เพื่อจับคู่การนับ break;หยุดลูปก่อนกำหนด;continue;ข้ามไปรอบถัดไป
Common mistakes
- Do not put a
;right afterfor(...)— it makes an empty loop. - Change the condition so the loop can end.
ข้อผิดพลาดที่พบบ่อย
- อย่าใส่
;ทันทีหลังfor(...)— จะทำให้เกิดลูปว่างเปล่า - ปรับเงื่อนไขเพื่อให้ลูปสามารถสิ้นสุดได้
Now you try
- Set up your accumulator or counter before the loop, update it inside, and use it after.
- For the function tasks, do not write a
main— the checker provides one.
ลองดูเลย
- ตั้งค่าตัวสะสมหรือตัวนับ ก่อน ลูป อัปเดตมัน ภายใน และใช้งานมัน หลังจาก ลูป
- สำหรับโจทย์เกี่ยวกับฟังก์ชัน อย่าเขียน
main— ตัวตรวจสอบจะจัดเตรียมให้เอง
Loops and accumulation · Loops และการสะสม
A loop repeats; a running total builds up each pass. · ลูปทำซ้ำ; ผลรวมสะสม สร้างขึ้นในแต่ละรอบ
In main, use a for loop to print the numbers 1 to 5, each on its own line. · ใน main, ใช้ลูป for เพื่อพิมพ์ตัวเลข 1 ถึง 5, แต่ละบรรทัดหนึ่งตัว
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete int sum_to(int n) so it returns 1 + 2 + ... + n. Return 0 when n is 0 or negative. Do not write a main. · เติมเต็ม int sum_to(int n) ให้ส่งกลับ 1 + 2 + ... + n. ส่งกลับ 0 เมื่อ n เป็น 0 หรือเป็นลบ. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete int count_multiples(int n, int k) so it returns how many numbers from 1 to n are divisible by k. Use % inside a loop. Do not write a main. · เติมเต็ม int count_multiples(int n, int k) ให้ส่งกลับจำนวนตัวเลขจาก 1 ถึง n ที่หารด้วย k ลงตัว. ใช้ % ภายในลูป. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่