การนำ Selection และ Iteration มาใช้
แนะนำใหม่
7:49
การเลือกและการทำซ้ำ
นี่คือลูปสามชุด สองชุดแรกแตกต่างกันเพียงหนึ่งตัวอักษร —เครื่องหมายน้อยกว่าแทนที่จะเป็นน้อยกว่าหรือเท่ากับ ลูปแรกทำงานสิบครั้ง ลูปที่สองทำงานสิบหนึ่ง…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| accumulator/əˈkjuːmjʊleɪtə/ | ตัวสะสม |
A zero initial maximum invents a result
- An array contains −8, −3 and −11. Starting max at zero leaves a maximum that was never in the array.
- For this nonempty array, start max at a[0]. Comparing the later values updates −8 to −3 and keeps it there.
![A zero initial maximum invents a result: for the array −8, −3, −11, starting max at 0 returns 0, a value never in the array; starting at a[0] gives −8, then −3, which stays; an empty array needs a separate check](/learn-media/ap_computer_science_a/assets/0209-max-start.png?v=1794225445)

Loop patterns: accumulate
- A loop pattern is a reusable recipe for a common task. The accumulator 累加器 pattern builds a running total or product across iterations.
- Start
sum = 0, thensum = sum + xinside the loop; the running total changes; it can decrease when a negative value is added. Same idea for a running count (count++) or a running max.
Conditionals inside loops
- Put an
ifinside a loop to act only on items that match. "Count the even numbers": loop, andif (x % 2 == 0) count++; - The loop visits every item; the
ifdecides which ones to process. Combining selection and repetition is the heart of most algorithms.
Finding a maximum
- To find the largest value, keep a
maxvariable and update it when you see something bigger. Check the array is nonempty, startmaxat its first value, then examine the rest. An unspecified “very small number” can still exceed every actual input. if (x > max) { max = x; }inside the loop. After the loop,maxholds the largest value seen.
รูปแบบตัวสะสม (Accumulator pattern)
sum สะสมผลข้ามรอบ; เริ่มที่ 0
เพื่อสร้างผลรวมสะสมในลูป ควรเริ่มต้นตัวสะสมไว้ที่...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ผลรวมเริ่มที่ 0; ผลคูณควรเริ่มที่ 1
เพื่อสร้างผลคูณสะสม ควรเริ่มต้นตัวสะสมไว้ที่...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การเริ่มผลคูณที่ 0 จะทำให้ผลลัพธ์เป็น 0 เสมอ; ให้เริ่มที่ 1
ในการนับจำนวนเลขคู่ขณะวนลูป คุณต้องใช้...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The if เลือกว่าลูปจะนับรายการใดบ้าง
การวางการเริ่มต้นของตัวสะสมภายในลูปจะทำให้รีเซ็ตมันทุกครั้งที่วน
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เริ่มต้นก่อนลูป ไม่ใช่ภายในลูป
เพื่อหยุดลูปทันทีเมื่อพบเป้าหมาย คุณสามารถใช้...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
break ออกจากรูปแบบซ้ำชั้นในที่สุด ตัวแปร BOOLEAN flag ยังจำเป็นต้องใช้ในเงื่อนไขรูปแบบซ้ำหรือตรรกะควบคุม; การกำหนดค่ามันเพียงอย่างเดียวไม่หยุดการดำเนินการ
Early exit with a flag or break
- Sometimes you can stop early once you've found what you need. A
booleanflag can record “found it” and be checked by the loop condition; assigning the flag alone does not exit a loop.breakexits the innermost loop immediately. - "Does the list contain a zero?" — stop at the first zero. Early exit saves work but keep the logic correct for the "not found" case.
Initialize the accumulator correctly, or every result is off. A sum starts at 0, a product at 1 (starting a product at 0 makes it always 0), a max at the first element after a nonempty check. An empty collection has no maximum unless the contract explicitly defines a separate outcome. And update it every matching iteration — a misplaced initialization inside the loop resets it each pass and ruins the total.
What is the maximum of the nonempty array {−8, −3, −11}?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
−3 is the greatest actual element. Starting at 0 produces an invalid invented candidate.
การอ่าน a[0] เป็นวิธีที่ถูกต้องในการเริ่มต้นค่าสูงสุดสำหรับอาร์เรย์ว่าง
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ไม่มีองค์ประกอบแรกในอาร์เรย์ว่าง ให้ตรวจสอบอินพุตหรือกำหนดผลลัพธ์เฉพาะสำหรับกรณีไม่มีค่าสูงสุด
มีจำนวนเต็มบวกกี่ตัวใน int array {-8, -3, -11}?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
มีเพียง -8 ที่เหลือเศษเป็นศูนย์เมื่อหารด้วย 2; ค่าลบก็สามารถเป็นจำนวนคู่ได้เช่นกัน
Counting evens in a loop over an array a:
int count = 0;for (int i = 0; i < a.length; i++) { if (a[i] % 2 == 0) count++; }- The loop visits each element; the
ifcounts only the even ones.
Carry the reasoning to a new case
- An empty array has no first element and needs a separate contract or check.
- The initial value must suit the operation: sum zero, product one, maximum a valid starting candidate.
Common loop patterns combine iteration and selection: an accumulator (sum += x, count, or running max) built across passes, with an if inside to process only matching items. Initialize the accumulator right (sum 0, product 1, max the first value), and consider an early exit (break/flag) once the answer is found.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้