Loops: while, for, and accumulation · Loops: while, for, และการสะสม
Loops
- A loop repeats a block of code many times.
- Loops let you count, add up numbers, and do work without copying lines.
- In this lesson you learn the
whileloop and theforloop.
Loops
- Loop ทำซ้ำชุดคำสั่งหลายครั้ง
- Loop ช่วยให้คุณนับจำนวน รวมนตัวเลข และทำงานต่างๆ โดยไม่ต้องคัดลอกบรรทัดซ้ำๆ
- ในบทเรียนนี้คุณจะได้เรียนรู้เรื่อง loop
whileและ loopfor
The while loop
- A
whileloop runs its block again and again while a condition istrue. - You need three parts: start a counter, test it, and change it inside the loop.
- If you never change the counter, the loop runs forever. Be careful.
The while loop
- Loop
whileจะรัน block ของโค้ด ซ้ำแล้วซ้ำเล่า ตราบใดที่เงื่อนไขยังคงtrue - คุณจำเป็นต้องมีสามส่วนหลัก: เริ่มต้น ตัวนับ, ทดสอบ ค่า, และ เปลี่ยนแปลง它在循环内。
- หากคุณไม่เปลี่ยนตัวนับเลย Loop จะรันไปตลอดกาล ระวังให้ดี
public class Main {
public static void main(String[] args) {
int i = 1; // start
while (i <= 3) { // test
System.out.println("Line " + i);
i = i + 1; // change
}
}
}
The for loop
- A
forloop puts the start, test, and change on one line. - The shape is
for (start; test; change) { ... }. - It does the same job as the
whileabove, but it is shorter and clearer.
The for loop
- Loop
for将 start, test และ change วางไว้ในบรรทัดเดียว - The shape is
for (start; test; change) { ... }. - มันทำหน้าที่เหมือนกับ
whileข้างต้น แต่สั้นกว่าและอ่านง่ายกว่า
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
System.out.println("Line " + i);
}
}
}
Accumulation
- Accumulation means building up an answer step by step inside a loop.
- Use a variable that starts at
0and grows:sum = sum + i;. i++is a short way to writei = i + 1.sum += iis short forsum = sum + i.
Accumulation
- Accumulation หมายถึงการสร้างคำตอบทีละขั้นตอนภายใน Loop
- Use a variable that starts at
0and grows:sum = sum + i;. i++เป็นวิธีเขียนสั้นๆ ของi = i + 1sum += iย่อมาจากsum = sum + i
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i; // add each number
}
System.out.println(sum); // 15
}
}
Counting with a condition
- You can count how many times something is true.
- Start a counter at
0, and add1only when the test passes. - Here we count even numbers from 1 to 10.
Counting with a condition
- คุณสามารถนับได้ว่า某件事发生了几次 true
- เริ่มตัวนับที่
0แล้วเพิ่ม1ก็ต่อเมื่อการทดสอบผ่านเท่านั้น - Here we count even numbers from 1 to 10.
public class Main {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) { // % gives the remainder
count++;
}
}
System.out.println(count); // 5
}
}
Off-by-one care
- The most common loop bug is doing one too many or one too few steps.
i <= nincludesn.i < nstops beforen.- To loop from 1 to
n(and includen), usefor (int i = 1; i <= n; i++).
Off-by-one care
- ข้อผิดพลาดที่พบบ่อยที่สุดใน Loop คือการทำขั้นตอน มากเกินไป หรือ น้อยเกินไป
i <= nincludesn.i < nstops beforen.- เพื่อวนจาก 1 ถึง
n(รวมnด้วย) ให้ใช้for (int i = 1; i <= n; i++)
public class Main {
public static void main(String[] args) {
int n = 4;
// i goes 1, 2, 3, 4 — four times, because of <=
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}
}
Common mistakes
for (int i = 0; i < n; i++)runsntimes;<=runs one extra.- Do not put a
;right afterfor(...).
ข้อผิดพลาดที่พบบ่อย
for (int i = 0; i < n; i++)runsntimes;<=runs one extra.- Do not put a
;right afterfor(...).
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
ลองดูเลย
- Each task เติม skeleton class ไว้ล่วงหน้า — เขียนโค้ดของคุณ ภายใน main, หรือเติมเต็ม method ที่แสดง
- กด Run เพื่อ compile และ run, แล้วกด Check answer
- โค้ดของคุณ compile และ run บน server, ดังนั้นแม้การ run ครั้งแรกก็เร็ว
Loops and accumulation · Loops และการสะสม
A loop repeats; an accumulator builds up across the passes. · Loop ทำซ้ำ; accumulator สร้างค่าสะสมผ่านรอบต่างๆ
Use a for loop to print the numbers 1 to 5, each on its own line. · ใช้ for loop เพื่อพิมพ์ตัวเลข 1 ถึง 5, แต่ละตัวอยู่บนบรรทัดของตัวเอง
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete sumTo(int n) so it returns the sum 1 + 2 + ... + n. For example sumTo(5) is 15. If n is 0 or less, return 0. · เติมเต็ม sumTo(int n) เพื่อให้ return ผลรวม 1 + 2 + ... + n. ตัวอย่างเช่น sumTo(5) ได้ผลเป็น 15. ถ้า n เป็น 0 หรือน้อยกว่า, return 0
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete countMultiples(int n, int k) so it returns how many numbers from 1 to n divide evenly by k (use % == 0). For example countMultiples(10, 3) is 3 (that is 3, 6, 9). · เติมเต็ม countMultiples(int n, int k) เพื่อให้ return ว่ามีจำนวนกี่จำนวนตั้งแต่ 1 ถึง n ที่หาร k ลงตัว (ใช้ % == 0). ตัวอย่าง countMultiples(10, 3) ได้ผลเป็น 3 (นั่นคือ 3, 6, 9)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่