Selection and iteration
แนะนำใหม่
23:18
พื้นฐานการเขียนโปรแกรม
โปรแกรมต้องขอรหัสผ่าน และจะขอรหัสซ้ำจนกว่าจะถูกต้อง ง่ายมาก — แต่จะเลือกใช้ลูปแบบใด? โปรแกรมมีการตัดสินใจและทำงานซ้ำกัน และการเลือก…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| security update | A change to software that fixes weaknesses attackers could use. |
| code/kəʊd/ | Instructions written in a programming language for a computer to follow. |
| certificate/səˈtɪfɪkət/ | A digital document that helps prove the identity of a website. |
| attacker | A person who tries to harm or gain unauthorised access to systems or data. |
| selection/sɪˈlekʃn/ | การเลือก |
| Iteration/ˌɪtəˈreɪʃn/ | Repeating a sequence of instructions or calculations. |
| constructs | Basic ways to organise steps in an algorithm, including order, choice and repetition. |
| trace table/treɪs ˈteɪbl/ | ตารางติดตาม |
| condition/kənˈdɪʃn/ | A requirement or circumstance that affects what happens or whether something is true. |
| Boolean expression/ˈbuːlɪən ekˈspreʃn/ | An expression whose value is true or false. |
| comparison/kəmˈpærɪsn/ | An operation that compares two values and gives a true or false result. |
| branch/bræntʃ/ | A path of instructions chosen according to a condition or value. |
| nested/ˈnestɪd/ | แบบซ้อนกัน |
| logic error/ˈlɒdʒɪk ˈerə/ | ความผิดพลาดด้านตรรกะ |
| boundary value | A value at the edge between two possible outcomes of a condition. |
| guard/ɡɑːd/ | A value, list or range that determines when a CASE branch runs. |
| assignment/əˈsaɪnmənt/ | Storing a value in a variable or other named location. |
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| block/blɒk/ | A group of instructions treated as a unit in a program. |
| count-controlled loop/kaʊnt kənˈtrəʊld luːp/ | ลูปควบคุมด้วยนับ |
| element/ˈelɪmənt/ | A substance made of atoms with the same proton number. |
| array/əˈreɪ/ | อาร์เรย์ |
| pre-condition loop/priː kənˈdɪʃn luːp/ | ลูปก่อนเงื่อนไข |
| post-condition loop/pəʊst kənˈdɪʃn luːp/ | ลูปหลังเงื่อนไข |
| loop body | The instructions inside a loop that are repeated when the loop runs. |
| counter/ˈkaʊntə/ | A device or variable that records how many events or items have occurred. |
| dry run/draɪ rʌn/ | การรันแบบแห้ง |
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| keyword/ˈkiːwɜːd/ | A word with a fixed meaning in a programming language or pseudocode. |
บรรทัดเกินมาหนึ่งบรรทัด
- ในปี 2014 Apple ส่งอัปเดตความปลอดภัยเนื่องจากบรรทัดซ้ำเพียงบรรทัดเดียว ลึกอยู่ในโค้ดที่ตรวจสอบ certificate ของเว็บไซต์,
goto fail;ปรากฏสองครั้งติดกัน - สำเนาที่สองอยู่นอก
IFของมัน, ดังนั้นจึงทำงานทุกครั้ง และการตรวจสอบด้านล่างไม่เคยทำงาน เป็นเวลาหลายเดือน ผู้โจมตีสามารถปลอมตัวเป็นเว็บไซด์ใดก็ได้ - Selection and iteration are the constructs that decide what runs and how often. Get one line of them wrong and the program still runs, just wrongly.
- บทเรียนนี้คือ IF, CASE และลูปทั้งสาม, และตาราง trace ที่จับข้อผิดพลาดได้ก่อนการสอบ

Selection
IF age >= 18 THEN … ELSE … ENDIF
ELSEเป็น option,ENDIFไม่ใช่ ทุกIFต้องปิด- Condition คือ Boolean expression ใดๆ: การเปรียบเทียบ, หรือการเปรียบเทียบที่เชื่อมด้วย
AND,ORและNOT

*An IF … ELSE ตรวจสอบครั้งเดียว แล้วทำงาน exactly หนึ่ง branch
Nested IFs และ boundaries
- A nested IF คือ IF ภายใน branch ของอีก IF หนึ่ง, และแต่ละตัวต้องการ
ENDIFของตัวเอง - ขอบเขตคือจุดที่คะแนนถูกหัก "คะแนน 50 ขึ้นไปผ่าน" คือ
Mark >= 50, ไม่ใช่Mark > 50. - การเปรียบเทียบที่ผิดเป็น ความผิดพลาดด้านตรรกะ: โปรแกรมรันได้แต่ให้คำตอบผิดสำหรับบางอินพุต การใช้ Trace กับค่าขอบเขต เช่น ตัวเลข 50 เอง เป็นวิธีหาข้อผิดพลาดนี้

"A mark of 50 or more passes" is written as IF Mark ____ 50 THEN.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
"Or more" includes 50 itself, so the comparison is greater than or equal. > would fail a student on exactly 50.
กรณี
CASE OF Grade … ENDCASEทดสอบ ค่าเดียว เทียบกับตัวเลือกหลายตัว และดูสะอาดกว่าการใช้ IF ซ้อนกันเป็นลูกโซ่- Guard อาจเป็นค่าเดียว
"A":, ลิสต์1, 2, 3:, หรือช่วง1 TO 5:Branchสุดท้ายสำหรับ "ทุกอย่างอื่น" คือOTHERWISE, ไม่ใช่เงื่อนไขเช่น> 200 - Guards ถูกทดสอบ ตามลำดับ เมื่อมี
1 TO 50:ตามด้วย40 TO 60:ค่า 45 จะเข้า branch แรก ทำให้การกำหนดค่าใน branch ที่สองอาจไม่เกิดขึ้นเลย หาก branch ก่อนหน้าครอบคลุมทุกค่าที่เป็นไปได้OTHERWISEก็จะไม่ทำงานเช่นกัน

CASE จะทำงาน branch แรก whose guard ตรงกับค่า
A CASE statement is cleaner than nested IFs when you are:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
CASE matches one value against many possibilities; deep nested IFs become hard to read.
Which of these are valid guards in a Cambridge CASE statement? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A single value, a value list, a range and OTHERWISE. A comparison such as > 200 is not a guard; anything not covered goes to OTHERWISE.
ตัวอย่างทำพร้อมกัน : เขียน CASE ใหม่โดยไม่ใช้ CASE
CASE OF MySwitchพร้อม1: ThisChar ← 'a',2: ThisChar ← 'y',3: ThisChar ← '7',OTHERWISE: ThisChar ← '*'- แต่ละค่าจะกลายเป็นหนึ่ง branch ของ chain IF, และ
OTHERWISEจะกลายเป็นELSEสุดท้าย
IF MySwitch = 1 THEN
ThisChar <- 'a'
ELSE
IF MySwitch = 2 THEN
ThisChar <- 'y'
ELSE
IF MySwitch = 3 THEN
ThisChar <- '7'
ELSE
ThisChar <- '*'
ENDIF
ENDIF
ENDIF
- IF สามตัว, ENDIF สามตัว การกลับทางกัน CASE สอง clause ที่กำหนดค่าเดียวกันรวมเป็น guard เดียวที่มีลิสต์ค่า,
1, 2:
ลูปทั้งสาม
- Iteration ทำซ้ำบล็อก, และลูปทั้งสามต่างกันที่ตำแหน่งที่เงื่อนไขถูกทดสอบ
- Count-controlled loop,
FOR i ← 1 TO 10 … NEXT i, รันจำนวนครั้งที่ทราบแล้ว;STEP -1นับถอยหลัง ใช้สำหรับจำนวนคงที่หรือทุก element ของ array - Pre-condition loop,
WHILE … DO … ENDWHILE, ทดสอบ ก่อน ทุกรอบ ดังนั้นอาจรันศูนย์ครั้ง Post-condition loop,REPEAT … UNTIL, ทดสอบ หลัง ทุก รอบ ดังนั้นรันอย่างน้อยหนึ่งครั้งเสมอ

*ตำแหน่งของเงื่อนไขกำหนดว่า body สามารถรันกี่ครั้ง
Match each loop to when you'd use it.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
FOR = count-controlled; WHILE = condition tested before (0+ passes); REPEAT = condition tested after (1+ passes).
REPEAT...UNTIL tests its condition AFTER the body, so the body always runs at least once.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
That post-condition test is the difference from WHILE, which can run zero times.
เลือกลูป: คำตอบสองคะแนน
- คะแนนหนึ่งสำหรับ ชื่อ, คะแนนหนึ่งสำหรับ เหตุผล, ในคำของโครงสร้างคะแนน
- Count-controlled, because the number of iterations is known before the loop starts. Post-condition, because the loop body must be executed at least once. Pre-condition, because the loop may not need to execute at all.
- การวนลูปเหนือสี่ elements ของ array ที่เขียนด้วย
WHILEที่มี counter บอกว่า "ไม่ใช่วิธีที่เหมาะสมที่สุด"; FOR loop เหมาะกว่า เพราะทราบจำนวน
A question asks you to "identify a more appropriate loop and justify your choice" for 2 marks. What earns the two marks?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
One mark for the loop, one for the reason: the count is known, the body must run once, or the loop may not run at all.
ตัวอย่างทำพร้อมกัน : ลูปไหนเหมาะกับแต่ละงาน?
- พิมพ์ตารางคูณ 12: จำนวนรู้ล่วงหน้า ดังนั้นใช้ FOR loop
- อ่านตัวเลขต่อไปจนผู้ใช้ใส่ 0: จำนวนไม่รู้และอินพุตแรกอาจเป็น 0 เลย ดังนั้นการทดสอบต้องอยู่เหนือ body: WHILE loop
- ขอรหัสผ่านจนถูกต้อง: ผู้ใช้ต้องถูกถามอย่างน้อยหนึ่งครั้ง ดังนั้นใช้ REPEAT … UNTIL loop
- Ask two questions: how many times, and must the body run before the first test?

"Keep asking for a password until it is correct, but always ask at least once." Which loop fits?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
You must ask at least once, so the post-condition REPEAT...UNTIL is the natural choice.
ตารางติดตามค่า
- Trace table บันทึกค่าของตัวแปรทุกตัวขณะคุณ dry run อัลกอริทึมด้วยมือ นี่คือวิธีทดสอบลูปบนกระดาษและเป็นคำถามหกคะแนนใน Paper 2 ส่วนใหญ่
- คอลัมน์ต่อหนึ่งตัวแปร, บวกกับคอลัมน์สำหรับ output ใด ๆ เขียนแถวใหม่เฉพาะเมื่อค่าเปลี่ยน, และประเมินเงื่อนไขลูป ณ วินาทีที่ลูปไปถึงมัน
- ไปต่อจนเงื่อนไขหยุดลูป, แล้วบันทึก output สุดท้าย
Trace a loop, pass by pass
A trace table records each variable after every pass of the loop. Watch the counter i climb while the running total builds up — exactly what an exam trace question asks you to fill in.
หลังจากนั้น total = 0; FOR i = 1 TO 5: total = total + i, what is the value of total?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Adding 1+2+3+4+5 = 15 — exactly what the trace table builds up pass by pass.
ตัวอย่างทำพร้อมกัน : ตาราง Trace ที่เติมแล้ว
Count <- 1
Total <- 0
WHILE Total < 10 DO
Total <- Total + Count
Count <- Count + 1
ENDWHILE
OUTPUT Count
- แถวของ (Count, Total): (1, 0) → (2, 1) → (3, 3) → (4, 6) → (5, 10)
- ที่ Total = 10 เงื่อนไข
Total < 10เป็นเท็จ, ลูปจบ, และ output คือ 5 - บรรทัดสุดท้ายเป็นบรรทัดที่ผิดบ่อยที่สุด: ตรวจสอบเงื่อนไขอีกครั้งหนึ่งก่อนเขียนผลลัพธ์

In the worked trace, the loop ends with Total = 10 and Count = 5, so the output is 5.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
After the fourth pass Total reaches 10, the condition Total < 10 becomes false, and Count has been incremented to 5.
คะแนนที่หลุดหายไป
>=และ>เป็นคำตอบต่างกัน ทดสอบค่าขอบเขตในใจก่อนเขียน ComparisonOTHERWISEเป็น keyword, ไม่ใช่ condition และทุกIF,CASE,WHILEและFORปิดด้วยENDIF,ENDCASE,ENDWHILEหรือNEXT- A
WHILEcan run zero times; aREPEATcannot. Choose by whether the body must run once before the first test. - ใน trace table, ค่าที่ไม่เปลี่ยนแปลงจะไม่ถูกเขียนทับ, และ output จะถูกเขียนก็ต่อเมื่อบรรทัด
OUTPUTทำงาน
คุณเข้าใจแล้ว
- IF … ELSE … ENDIF สำหรับทางเลือก; nested IFs ปิดแต่ละตัว; CASE สำหรับค่าเดียวเทียบกับหลาย guards, ทดสอบตามลำดับ,
OTHERWISEล่าสุด - FOR จำนวนทราบ · WHILE ทดสอบก่อน, อาจรันศูนย์ครั้ง · REPEAT … UNTIL ทดสอบหลัง, รันอย่างน้อยหนึ่งครั้ง
- อธิบายเหตุผลลูปด้วย ชื่อและเหตุผล: จำนวนทราบ, อาจไม่รัน, ต้องรันหนึ่งครั้ง
- trace table dry-run อัลกอริทึมแถวละหนึ่งต่อการเปลี่ยนแปลง; ตรวจสอบเงื่อนไขอีกครั้งก่อน output สุดท้าย
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้