อัลกอริทึมและ伪代码 (Pseudocode)
แนะนำใหม่
14:30
การคิดเชิงคำนวณ
นี่คืองาน: สร้างระบบเพื่อจัดการสต็อกของร้านทั้งหมด — สินค้าทุกชิ้น การขายทุกครั้ง การจัดส่งทุกครั้ง รายงานทุกฉบับ ในฐานะปัญหาใหญ่เดียว มัน…**
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| algorithm/ˈælɡərɪθəm/ | อัลกอริทึม |
| sequence/ˈsiːkwəns/ | ลำดับ |
| deterministic/dɪˌtɜːmɪˈnɪstɪk/ | แบบกำหนดได้ |
| identifier table/aɪˈdentɪfaɪə ˈteɪbl/ | ตารางตัวระบุ |
| variable/ˈveərɪəbl/ | ตัวแปร |
| data type/ˈdeɪtə taɪp/ | ชนิดข้อมูล |
| pseudocode/ˈsuːdəʊkəʊd/ | รหัสจำลอง |
| assignment/əˈsaɪnmənt/ | การกำหนดค่า |
| loop/luːp/ | ลูป |
| count-controlled loop/kaʊnt kənˈtrəʊld luːp/ | ลูปควบคุมด้วยนับ |
| pre-condition loop/priː kənˈdɪʃn luːp/ | ลูปก่อนเงื่อนไข |
| post-condition loop/pəʊst kənˈdɪʃn luːp/ | ลูปหลังเงื่อนไข |
| iteration/ˌɪtəˈreɪʃn/ | รอบการทำงาน |
| selection/sɪˈlekʃn/ | การเลือก |
| flowchart/ˈfləʊtʃɑːt/ | แผนภาพไหล |
| stepwise refinement/ˈstepwaɪz rɪˈfaɪnmənt/ | การปรับแต่งทีละขั้น |
The most expensive hyphen in history
- On 22 July 1962 the Mariner 1 rocket, bound for Venus, was blown up 293 seconds after launch.
- The cause was one missing bar over a symbol in the guidance equations. The computer followed the written steps exactly, and the written steps were wrong.
- A computer never fills in what you meant. Every step you give it must have exactly one meaning.
- That is why this lesson is about writing steps a machine can follow: algorithms 算法.
What an algorithm is
- An algorithm is a solution to a problem expressed as a sequence of defined steps.
- An algorithm should have unambiguous steps (each has one meaning), be finite (it ends) and effective (each step can actually be done). A deterministic 确定性 algorithm follows the same steps for the same input; a randomised algorithm can behave differently.
- It says what to do, independent of any programming language, and every one follows input → process → output.

Every algorithm has the same shape: input, process, output
อัลกอริทึมเป็น "deterministic" (มีกำหนดผลลัพธ์) ซึ่งหมายความว่า:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Deterministic = อินพุตเดียวกัน → เอาต์พุตเหมือนกันทุกครั้ง (Finite = ขั้นตอนสิ้นสุด; unambiguous = ความหมายเดียวต่อขั้นตอน)
Worked example: the identifier table
- Before writing code, list every piece of data in an identifier table 标识符表: its variable 变量 name, its data type 数据类型 and a description.
- A shop's stock program stores
"Fruit",20/02/2025,12.67andTRUE. The exam asks for a name and a type for each. Category : STRING(a category of stock),DateSold : DATE(when it was sold),ItemCost : REAL(the cost),InStock : BOOLEAN(is it in stock?).- One mark per row for the name and the type, so write the type exactly as the pseudocode guide does:
INTEGER,REAL,STRING,CHAR,BOOLEAN,DATE.

An identifier table names every piece of data before you write code
ในตารางไถ่ (identifier table) ประเภทข้อมูลสำหรับค่าเช่น 12.67 (ค่าใช้จ่าย) คือ ____
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวเลขที่มีส่วนทศนิยมคือ REAL INTEGER สำหรับจำนวนเต็ม STRING สำหรับข้อความ BOOLEAN สำหรับ TRUE/FALSE และ DATE สำหรับวันที่
The three constructs
IF … THEN … ELSE … ENDIF
- Assignment 赋值 stores a value with an arrow,
Total ← Total + Value;=is for comparison.DIVis whole-number division andMODthe remainder, so17 MOD 5 = 2.
INPUT Age # sequence
IF Age >= 18 THEN
# selection
OUTPUT "Adult"
ELSE
OUTPUT "Minor"
ENDIF
FOR Count <- 1 TO 10 # iteration
OUTPUT Count
NEXT Count

The three building blocks of any algorithm
การเลือก: ตามเส้นทาง IF / ELSE
ลากคะแนนและดูว่าเส้นทางไหนทำงาน การเลือกจะทดสอบเงื่อนไขทีละข้อและเลือกเส้นทางที่ TRUE เป็นข้อแรก — นั่นคือวิธีการทำงานของ IF … ELSE IF … ELSE
จับคู่โครงสร้างการเขียนโปรแกรมทั้งสามกับสิ่งที่มันทำหน้าที่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
อัลกอริทึมทุกตัวสร้างจากโครงสร้างพื้นฐานเพียงสามอย่าง — ลำดับขั้นตอน, การเลือก และการทำซ้ำ
ใน伪代码นี้ สัญลักษณ์ใดหมายถึงการกำหนดค่า (เก็บค่า)?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การกำหนดค่าใช้ ← (เช่น x ← 5); = สงวนไว้สำหรับการเปรียบเทียบเท่านั้น
ค่าของ 17 MOD 5 คืออะไร?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
MOD ให้เศษเหลือ: $17 = 3 \times 5 + 2$, ดังนั้น 17 MOD 5 = 2. (17 DIV 5 = 3.)
Which loop?
FOR … NEXTwhen you know how many times: a count-controlled loop 计数循环.WHILE … ENDWHILEtests the condition before each pass, so the body may run zero times: a pre-condition loop 前测循环.REPEAT … UNTILtests after each pass, so the body always runs at least once: a post-condition loop 后测循环. Validating an input is the classic case.- A "describe the iteration construct" answer names the loop, says where the condition is tested, and gives the consequence.

A WHILE loop tests before the body runs; a REPEAT … UNTIL loop tests after it
WHILE loop แตกต่างจาก REPEAT...UNTIL loop อย่างไร?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
WHILE ตรวจสอบก่อน (สามารถทำงาน 0 ครั้ง); REPEAT...UNTIL ตรวจสอบหลัง ทำให้งานอย่างน้อยหนึ่งครั้งเสมอ
FOR loop เป็นแบบควบคุมนับจำนวน (ทำซ้ำจำนวนครั้งคงที่) ในขณะที่ WHILE loop เป็นแบบควบคุมโดยเงื่อนไข (ทำซ้ำจนกว่าเงื่อนไขจะเปลี่ยน)
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ใช้ FOR เมื่อคุณทราบจำนวนรอบที่จะวน; ใช้ WHILE/REPEAT เมื่อวนจนกว่าสิ่งใดสิ่งหนึ่งจะเป็นจริง
Worked example: from words to pseudocode
- Task: input 100 integer values, add up only the positive ones, and output the total.
- Plan the data first:
Count,TotalandNextNumber, allINTEGER. Then the three constructs do the rest.
DECLARE Count, Total, NextNumber : INTEGER
Total <- 0
FOR Count <- 1 TO 100
INPUT NextNumber
IF NextNumber > 0 THEN
Total <- Total + NextNumber
ENDIF
NEXT Count
OUTPUT Total
- The follow-up asks you to identify the constructs: iteration (the
FORloop repeats the input 100 times), selection (theIFdecides whether a value is added) and sequence (the statements run in order).
Spotting constructs in an extract
- A favourite question shows five pseudocode extracts and asks you to tick which of assignment, selection, iteration each one uses.
Result ← CalculateTotal()is an assignment.WHILE IsClosedis iteration.REPEAT … INPUT Value … UNTIL Sales[4] > Valueis iteration and assignment (INPUTstores a value). IF Sales[Current] <= 150 THEN Discount ← TRUE ENDIF- Look at every line of the extract, not only the first one. A row may need two ticks.
ส่วนนี้ใช้โครงสร้างใด? REPEAT … INPUT Value … UNTIL Total > 100. เลือก ทุก ข้อที่ใช้ได้
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
REPEAT … UNTIL เป็นการทำซ้ำ และ INPUT Value เก็บค่า ซึ่งถือเป็น Assignment ไม่มี IF หรือ CASE ดังนั้นจึงไม่มี Selection — เงื่อนไข UNTIL ควบคุมการวนลูป ไม่ได้เลือกระหว่างทางแยก
Flowcharts
- A flowchart 流程图 documents the same algorithm as a picture. Ovals are
STARTandEND, rectangles are processes, parallelograms areINPUT/OUTPUT, and a diamond is a decision. - A diamond is where selection happens, and a flow line that goes back up the chart is a loop.
- The exam asks both ways: pseudocode from a flowchart, and a flowchart from pseudocode or structured English. Every symbol you draw should map to one line of pseudocode.

Each flowchart symbol maps to one kind of pseudocode statement
ใน Flowchart รูปสี่เหลี่ยมขนมเปียกปูนแทนอะไร?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
รูปสี่เหลี่ยมจตุรัสใช้สำหรับการเลือก (Selection) และรูปสี่เหลี่ยมจตุรัสที่มีเส้นไหลย้อนกลับขึ้นไปในแผนภาพคือLoop Test ส่วนสี่เหลี่ยมผืนผ้าใช้สำหรับกระบวนการ สี่เหลี่ยมขนมเปียกปูนใช้สำหรับข้อมูลเข้า/ออก และวงรีใช้สำหรับจุดเริ่มต้นและจุดสิ้นสุด
Worked example: the guessing game
- The program picks a random integer from 1 to 100, then asks for guesses until the user gets it. The user must guess at least once, so the loop is a
REPEAT … UNTIL.
DECLARE Target, Guess : INTEGER
Target <- INT(RAND(100)) + 1
REPEAT
INPUT Guess
IF Guess < Target THEN
OUTPUT "Too low"
ELSE
IF Guess > Target THEN
OUTPUT "Too high"
ENDIF
ENDIF
UNTIL Guess = Target
OUTPUT "Correct"
- Follow the flowchart: one decision diamond for the loop test, two for the hints, and every flow line ends up back at
INPUT Guessor atEND.

The guessing game as a flowchart: the loop returns to the input until the guess matches
ทำไม REPEAT … UNTIL จึงเป็นLoop ที่ถูกต้องสำหรับเกมทายเลข?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Loop แบบPost-condition จะทำงานภายในLoop อย่างน้อยหนึ่งรอบก่อนทำการทดสอบ ซึ่งสอดคล้องกับเกมที่ต้องการให้ทายอย่างน้อยหนึ่งครั้ง ส่วนWHILE Loop จำเป็นต้องมีการทายก่อนเริ่มLoop เพื่อมีค่าให้ทดสอบ
Stepwise refinement
- Stepwise refinement 逐步求精 means starting from an outline and expanding each step into more detailed steps, again and again, until every step can be written directly as pseudocode.
- "Process an order" → "get the items", "calculate the total", "take payment" → "calculate the total" becomes "for each item, add price × quantity; apply any discount".
- Each level is a refinement of the one above, and the finished levels together are the design. "Describe stepwise refinement" wants the outline, the expansion and the stopping rule.

Refine each step until it can be coded directly
เรียงลำดับขั้นตอนของการปรับปรุงทีละขั้น (Stepwise Refinement)
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
วางโครงร่างก่อน แล้วจึงปรับปรุงทีละระดับ; หยุดเมื่อขั้นตอนใดเป็นPseudocode ได้เพียงบรรทัดเดียว
Logic statements
- Parts of a solution are defined by logic statements: conditions built from comparisons (
=,<>,<,>,<=,>=) joined byAND,ORandNOT. - A valid mark:
Mark >= 0 AND Mark <= 100. A discount applies if the customer is a member or spends over 50:IsMember OR Total > 50. NOT (Mark < 40)says the same thing asMark >= 40. Write the statement, then test it with a value on each side of the boundary.

Comparisons joined by AND, OR and NOT build the conditions an algorithm needs
NOT (Mark < 40) เป็นจริงสำหรับค่าของ Mark เดียวกันพอดีกับ Mark >= 40
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การปฏิเสธ "น้อยกว่า 40" ได้ผลลัพธ์เป็น "40 ขึ้นไป" ทดสอบขอบเขต: เมื่อ Mark = 40 จะทำให้ Mark < 40 เป็นเท็จ ดังนั้นส่วนกลับของมันจึงเป็นจริง และ 40 >= 40 ก็เป็นจริงด้วย
Marks that slip away
←assigns and=compares.IF Total = 0is a test;Total = 0on its own line earns nothing.- Every construct closes:
ENDIF,ENDWHILE,UNTIL,NEXT,ENDCASE. A missing closer breaks the structure mark. - Declare before you use, and initialise a running total to
0. WHILEmay never run,REPEATalways runs once. Choose the loop that matches the task, and say why if asked.
You've got it
- an algorithm's steps are unambiguous, deterministic, finite, effective; plan the data in an identifier table
- three constructs: sequence, selection (
IF/CASE), iteration (FOR/WHILE/REPEAT);WHILEtests before,REPEATafter - a flowchart and pseudocode describe the same algorithm; stepwise refinement expands an outline until it can be coded
- conditions are logic statements: comparisons joined with
AND,OR,NOT
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้