Loops: while, for, และการสะสม
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
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.
English
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.
English
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.
English
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
System.out.println("Line " + i);
}
}
}
English
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.
English
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
}
}
English
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.
English
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.- เพื่อวนจาก 1 ถึง
n(รวมnด้วย) ให้ใช้for (int i = 1; i <= n; i++)
English
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);
}
}
}
ข้อผิดพลาดที่พบบ่อย
for (int i = 0; i < n; i++)runsntimes;<=runs one extra.- Do not put a
;right afterfor(...).
ลองดูเลย
- Each task เติม skeleton class ไว้ล่วงหน้า — เขียนโค้ดของคุณ ภายใน main, หรือเติมเต็ม method ที่แสดง
- สำหรับโปรแกรมที่สมบูรณ์ Run จะคอมไพล์และรันมัน สำหรับงาน method หรือ class Check answer จะคอมไพล์โค้ดของคุณด้วยการทดสอบที่จัดเตรียมไว้และแสดงผล
- โค้ดของคุณ compile และ run บน server, ดังนั้นแม้การ run ครั้งแรกก็เร็ว
สำรวจ
Loops และการสะสม
Loop ทำซ้ำ; accumulator สร้างค่าสะสมผ่านรอบต่างๆ
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ใช้ for loop เพื่อพิมพ์ตัวเลข 1 ถึง 5, แต่ละตัวอยู่บนบรรทัดของตัวเอง
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เติมเต็ม sumTo(int n) เพื่อให้ return ผลรวม 1 + 2 + ... + n. ตัวอย่างเช่น sumTo(5) ได้ผลเป็น 15. ถ้า n เป็น 0 หรือน้อยกว่า, return 0
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เติมเต็ม countMultiples(int n, int k) เพื่อให้ return ว่ามีจำนวนกี่จำนวนตั้งแต่ 1 ถึง n ที่หาร k ลงตัว (ใช้ % == 0). ตัวอย่าง countMultiples(10, 3) ได้ผลเป็น 3 (นั่นคือ 3, 6, 9)
คลิก Run เพื่อดูผลลัพธ์ที่นี่