Flowchart Basic — Planning Before You Code Flowchart พื้นฐาน — วางแผนก่อนเขียนโค้ด
Learn why flowcharts matter, the eight ANSI/ISO symbols, and how each one maps to program logic. เรียนรู้ความสำคัญของ flowchart สัญลักษณ์ ANSI/ISO 8 ชนิด และการเชื่อมแต่ละสัญลักษณ์กับ logic ของโปรแกรม
A flowchart is a visual diagram of the steps in a program. Long lists of instructions are hard to follow; a diagram shows the order at a glance. Drawing one before you write Python helps you plan the control flow — the sequence of actions, decisions, and loops.
Flowchart คือแผนภาพขั้นตอนของโปรแกรม รายการคำสั่งยาว ๆ อ่านยาก แต่แผนภาพแสดงลำดับได้ทันที การวาด flowchart ก่อน เขียน Python ช่วยวางแผน control flow คือลำดับการทำงาน การตัดสินใจ และการวนซ้ำ
A good flowchart maps cleanly to structured programming: sequence (do steps in order), selection (choose a path with if/else), and iteration (repeat with loops). Each block has a clear entry and exit, so the logic is easier to check and explain to others.
Flowchart ที่ดีแปลเป็น structured programming ได้ชัดเจน: sequence (ทำตามลำดับ), selection (เลือกเส้นทางด้วย if/else) และ iteration (วนซ้ำด้วย loop) แต่ละบล็อกมีจุดเข้าและจุดออกชัด จึงตรวจ logic และอธิบายให้ผู้อื่นเข้าใจได้ง่าย
| Shapeรูปทรง | Nameชื่อ | Descriptionคำอธิบาย |
|---|---|---|
| TerminatorTerminator | Start or stop of the program (or sub-chart).จุดเริ่มหรือจบของโปรแกรม (หรือ sub-chart) | |
| Data (Input / Output)Data (Input / Output) | Read data in or write results out.รับข้อมูลเข้าหรือส่งผลลัพธ์ออก | |
| DecisionDecision | A yes/no or true/false test; two exit paths.ทดสอบ Yes/No หรือ True/False มีสองเส้นทางออก | |
| ProcessProcess | A calculation or action that changes data.การคำนวณหรือการกระทำที่เปลี่ยนข้อมูล | |
| Sub-processSub-process | Call another module or function defined elsewhere.เรียกโมดูลหรือฟังก์ชันที่นิยามไว้ที่อื่น | |
| On-page connectorOn-page connector | Jump to a matching circle on the same page (labelled A, B, …).กระโดดไปวงกลมคู่ในหน้าเดียวกัน (ใส่ label A, B, …) | |
| Off-page connectorOff-page connector | Continue on another page (pentagon points to the exit).ต่อในหน้าอื่น (รูปห้าเหลี่ยมชี้ทิศทางออก) | |
| FlowlineFlowline | Arrow showing the order of execution between symbols.ลูกศรแสดงลำดับการทำงานระหว่างสัญลักษณ์ |
The terminator is a rounded rectangle (pill) labelled START or STOP. It marks where control enters and leaves a chart.
Terminator เป็นรูปสี่เหลี่ยมมุมโค้ง (pill) ที่เขียนว่า START หรือ STOP ใช้ทำเครื่องหมายจุดที่การทำงานเข้าและออกจากแผนภาพ
How it works: every main flowchart has exactly one START and one STOP. A separate sub-chart for a function also opens and closes with terminators — the sub-chart for find_total() begins at its own START and ends with Return, handing control back to the caller.
การทำงาน: ทุก flowchart หลักมี START หนึ่งจุดและ STOP หนึ่งจุด sub-chart ของฟังก์ชันก็เปิดและปิดด้วย terminator เช่นกัน — sub-chart ของ find_total() เริ่มที่ START ของตัวเอง และจบด้วย Return เพื่อส่งการทำงานกลับไปยังผู้เรียก
240
The parallelogram marks input or output. Input reads a value from outside the program (keyboard, file, sensor). Output sends a result back (screen, printer, file). In Python these become input() and print().
สี่เหลี่ยมด้านขนาน (parallelogram) ใช้กับ input หรือ output Input อ่านค่าจากภายนอกโปรแกรม (คีย์บอร์ด ไฟล์ เซนเซอร์) Output ส่งผลลัพธ์ออก (หน้าจอ เครื่องพิมพ์ ไฟล์) ใน Python มักเป็น input() และ print()
How it works: the flow reads a value in through an Input parallelogram, then later writes the result out through an Output parallelogram — the shape is the same, only the direction of the data changes.
การทำงาน: การไหลรับค่าเข้ามาผ่าน parallelogram แบบ Input แล้วจึงส่งผลลัพธ์ออกผ่าน parallelogram แบบ Output — รูปทรงเหมือนกัน ต่างกันแค่ทิศทางของข้อมูล
25 77.0
The diamond holds a condition with two labelled exits, usually Yes and No (or True / False). Only one path is taken.
รูปสี่เหลี่ยมข้าวหลามตัด (diamond) ใส่เงื่อนไขที่มีทางออกสองทาง มักเป็น Yes และ No (หรือ True / False) เลือกได้เพียงเส้นทางเดียว
How it works: read a number, test number % 2 == 0. Yes → output “even”; No → output “odd”. Both branches rejoin before STOP.
การทำงาน: อ่านค่า number แล้วทดสอบ number % 2 == 0 ถ้า Yes → แสดง “even” ถ้า No → แสดง “odd” ทั้งสองเส้นทางมารวมกันก่อนถึง STOP
odd
A rectangle is a process step: assign a value, compute a formula, or update a variable. No branching — execution continues straight down to the next symbol.
สี่เหลี่ยม (rectangle) คือขั้นตอน process: กำหนดค่า คำนวณสูตร หรืออัปเดตตัวแปร ไม่มีการแตกแขนง — ทำงานต่อลงสัญลักษณ์ถัดไปทันที
How it works: read Celsius C, run one process block F = (float(C) * 1.8) + 32, then output F.
การทำงาน: อ่านค่าองศาเซลเซียส C ทำ process หนึ่งบล็อก F = (float(C) * 1.8) + 32 แล้วแสดงผล F
212.0
A rectangle with double side-bars is a predefined process: “run the steps in another chart or function.” The main chart stays short; the detail lives in a sub-chart.
สี่เหลี่ยมที่มีขีดสองข้าง คือ predefined process: “รันขั้นตอนในแผนภาพหรือฟังก์ชันอื่น” แผนหลักจึงสั้น ส่วนรายละเอียดอยู่ใน sub-chart
How it works: the main chart reads a list of scores, then calls total = find_total(scores) as one predefined-process box and displays the result. The loop that actually adds the numbers is drawn separately (see card 10).
การทำงาน: แผนหลักอ่านลิสต์คะแนน แล้วเรียก total = find_total(scores) เป็นกล่อง predefined process หนึ่งกล่อง และแสดงผลลัพธ์ ส่วน loop ที่บวกตัวเลขจริง ๆ วาดแยกไว้ต่างหาก (ดู card 10)
240
When two paths would need long or crossing arrows on one page, a pair of labelled circles replaces the line. Both circles share the same letter (A, B, …).
เมื่อสองเส้นทางต้องใช้ลูกศรยาวหรือไขว้กันในหน้าเดียว ใช้วงกลมคู่ที่มี label แทนเส้น วงกลมทั้งคู่ใช้ตัวอักษรเดียวกัน (A, B, …)
How it works: here both branches of a decision must reach STOP. Instead of drawing two arrows that cross, each branch ends at a circle A; a single circle A picks the flow back up and continues to STOP.
การทำงาน: ในตัวอย่างนี้ทั้งสองแขนงของ decision ต้องไปถึง STOP แทนที่จะวาดลูกศรสองเส้นไขว้กัน ให้แต่ละแขนงจบที่วงกลม A แล้ววงกลม A อีกจุดรับการไหลต่อไปยัง STOP
An off-page connector is a pentagon with a page number or label. It shows that the flow continues on another sheet; the matching pentagon on the next page is the entry point.
Off-page connector เป็นรูปห้าเหลี่ยมพร้อมเลขหน้าหรือ label แสดงว่าการไหลต่อในหน้าถัดไป pentagon คู่ในหน้านั้นคือจุดเข้า
How it works: a large chart is split across pages. Page 1 ends with pentagon 2; page 2 begins with pentagon 2 and carries on to STOP. The practice programs below (cards 12–15) use exactly this to break their charts into two pages.
การทำงาน: แผนภาพใหญ่ถูกแบ่งเป็นหลายหน้า หน้า 1 จบด้วย pentagon 2 หน้า 2 เริ่มด้วย pentagon 2 แล้วทำงานต่อไปจนถึง STOP โปรแกรมฝึกด้านล่าง (card 12–15) ใช้วิธีนี้แบ่งแผนภาพออกเป็นสองหน้าพอดี
A for loop repeats a block once for each item in a list. In a flowchart it is a decision that checks a counter (i < len(scores)) with a loop-back arrow: while the test is Yes, run the body and return to the test; when it is No, leave the loop.
for loop ทำบล็อกซ้ำหนึ่งครั้งต่อหนึ่งสมาชิกในลิสต์ ใน flowchart คือ decision ที่ตรวจตัวนับ (i < len(scores)) พร้อมลูกศร ย้อนกลับ: ตราบใดที่ผลเป็น Yes ให้ทำ body แล้วกลับไปตรวจใหม่ เมื่อเป็น No จึงออกจาก loop
How it works: find_total(scores) starts with total = 0 and i = 0. Each pass adds scores[i] to total and increases i. When i reaches the list length, it returns total.
การทำงาน: find_total(scores) เริ่มด้วย total = 0 และ i = 0 แต่ละรอบบวก scores[i] เข้ากับ total แล้วเพิ่ม i เมื่อ i ถึงความยาวลิสต์ก็คืนค่า total
418
A while loop repeats as long as a condition is true — the number of passes is not known in advance. A common pattern is a boolean flag that the loop body switches off once the work is done.
while loop ทำซ้ำ ตราบใดที่เงื่อนไขเป็นจริง — ไม่รู้จำนวนรอบล่วงหน้า รูปแบบที่พบบ่อยคือใช้ flag แบบ boolean ที่ body ของ loop ปิดมันเองเมื่อทำงานเสร็จ
How it works: the flag While_loop_status starts True. Each pass the decision While_loop_status == True? is checked — No goes straight to Stop. On Yes, read an entry, then test Entry.isdigit()?. If No, print “Please type the numbers.” and loop back to the flag test. If Yes, print the entry, set While_loop_status = False, and loop back — now the flag test fails and the program stops.
การทำงาน: flag While_loop_status เริ่มเป็น True ทุกรอบจะตรวจ decision While_loop_status == True? — ถ้า No ไปที่ Stop ทันที ถ้า Yes ให้อ่าน entry แล้วทดสอบ Entry.isdigit()? ถ้า No แสดง “Please type the numbers.” แล้วย้อนกลับไปตรวจ flag ถ้า Yes แสดงค่า entry ตั้ง While_loop_status = False แล้วย้อนกลับ — คราวนี้เงื่อนไข flag เป็นเท็จ โปรแกรมจึงหยุด
Please type the numbers. Please type the numbers. 42
Practice: Flowchart and Runnable Python ฝึกหัด: Flowchart และ Python ที่รันได้
Read 5 test scores into a list, then report the total, average, highest, and lowest using four user-defined functions — find_total, find_average, find_highest, find_lowest. Each function is a small sub-chart of its own.
รับคะแนนสอบ 5 ค่าเข้าลิสต์ แล้วรายงาน ผลรวม ค่าเฉลี่ย ค่ามากสุด และ ค่าน้อยสุด โดยใช้ฟังก์ชันที่นิยามเอง 4 ตัว — find_total, find_average, find_highest, find_lowest แต่ละฟังก์ชันมี sub-chart ของตัวเอง
Main program โปรแกรมหลัก
How the main flow works: page 1 is a counting loop — while i < 5, ask for a score and store it, then add 1 to i. When the loop ends it passes through connector 2. Page 2 calls the four functions in turn as predefined processes and displays every result.
การทำงานของ flow หลัก: หน้า 1 เป็น loop นับรอบ — ตราบใดที่ i < 5 ให้ถามคะแนนและเก็บไว้ แล้วบวก 1 ที่ i เมื่อ loop จบก็ผ่าน connector 2 หน้า 2 เรียกฟังก์ชันทั้งสี่ทีละตัวเป็น predefined process แล้วแสดงผลทุกค่า
418 83.6 90 75
find_total(scores)
Adds every score with a counter loop: start total = 0, then for each index add scores[i] and step i on. Return total when the counter reaches the list length.
บวกทุกคะแนนด้วย loop นับรอบ: เริ่ม total = 0 แล้วแต่ละดัชนีบวก scores[i] และเดิน i ต่อ คืน total เมื่อตัวนับถึงความยาวลิสต์
418
find_average(scores)
Divides the total by how many scores there are. It has no loop of its own — it calls find_total and then one division.
หารผลรวมด้วยจำนวนคะแนน ไม่มี loop ของตัวเอง — เรียก find_total แล้วหารหนึ่งครั้ง
The loop inside find_total(scores) is shown above — jump to it. loop ภายใน find_total(scores) แสดงไว้ด้านบน — กดเพื่อเลื่อนไปดู
83.6
find_highest(scores)
Starts highest at float('-inf') so the first real score always beats it, then keeps the biggest score seen so far. An inner decision score > highest decides whether to replace it.
ตั้ง highest เป็น float('-inf') เพื่อให้คะแนนจริงตัวแรกมากกว่าเสมอ แล้วเก็บคะแนนที่มากที่สุดที่เจอ decision ภายใน score > highest ตัดสินว่าจะแทนค่าไหม
90
find_lowest(scores)
The mirror of find_highest: start at float('inf') and keep the smallest score, testing score < lowest.
กลับด้านของ find_highest: เริ่มที่ float('inf') และเก็บคะแนนที่น้อยที่สุด โดยทดสอบ score < lowest
75
Generate 5 random integers from 1–20, show them, and ask the user for the largest and smallest. find_largest(numbers) and find_smallest(numbers) compute the real answers; the program then checks each guess in turn.
สุ่มจำนวนเต็ม 5 ตัวจาก 1–20 แสดงให้ดู แล้วถามผู้ใช้ว่าตัวมากที่สุดและน้อยที่สุดคืออะไร find_largest(numbers) และ find_smallest(numbers) หาคำตอบจริง แล้วโปรแกรมตรวจคำตอบทีละข้อ
Main program โปรแกรมหลัก
How the main flow works: page 1 builds the random list, displays it, and reads the two guesses, then hands over through connector 2. Page 2 computes Largest_number and Smallest_number, then checks the largest guess and the smallest guess one after the other — each check prints “Correct!” or “Incorrect!” before both paths merge and stop.
การทำงานของ flow หลัก: หน้า 1 สร้างลิสต์สุ่ม แสดงผล และอ่านคำตอบสองข้อ แล้วส่งต่อผ่าน connector 2 หน้า 2 คำนวณ Largest_number และ Smallest_number แล้วตรวจคำตอบตัวมากและตัวน้อย ทีละข้อต่อกัน — แต่ละข้อแสดง “Correct!” หรือ “Incorrect!” ก่อนที่ทั้งสองเส้นทางจะมารวมกันแล้วหยุด
numbers: [1, 3, 20, 15, 13] largest: Correct! smallest: Incorrect! it was 1
find_largest(numbers)
Starts largest at float('-inf') and keeps the biggest number seen, testing number > largest each pass.
ตั้ง largest เป็น float('-inf') และเก็บตัวเลขที่มากที่สุดที่เจอ โดยทดสอบ number > largest ทุกรอบ
20
find_smallest(numbers)
The mirror of find_largest: start at float('inf') and keep the smallest number, testing number < smallest.
กลับด้านของ find_largest: เริ่มที่ float('inf') และเก็บตัวเลขที่น้อยที่สุด โดยทดสอบ number < smallest
1
Ask for the prices of 6 items, store them in a list, then report the total cost, the average item price, and the most expensive item using calculate_total, calculate_average, and find_most_expensive.
ถามราคาสินค้า 6 ชิ้น เก็บในลิสต์ แล้วรายงานราคารวม ราคาเฉลี่ยต่อชิ้น และชิ้นที่แพงที่สุด โดยใช้ calculate_total, calculate_average และ find_most_expensive
Main program โปรแกรมหลัก
How the main flow works: page 1 is a counting loop — while i < 6, ask for a price, append it, add 1 to i. When the loop ends it passes through connector 2. Page 2 calls the three functions and displays the results.
การทำงานของ flow หลัก: หน้า 1 เป็น loop นับรอบ — ตราบใดที่ i < 6 ให้ถามราคา เพิ่มเข้าลิสต์ แล้วบวก 1 ที่ i เมื่อ loop จบก็ผ่าน connector 2 หน้า 2 เรียกฟังก์ชันสามตัวและแสดงผล
63.0 10.5 15.75
User-defined functions — already drawn ฟังก์ชันที่นิยามเอง — วาดไว้แล้ว
All three helpers are the same shapes you saw in Example program 1 — only the list name changes. Jump to each flowchart:
ตัวช่วยทั้งสามใช้รูปเดียวกับที่เห็นใน ตัวอย่างโปรแกรม 1 — เปลี่ยนแค่ชื่อลิสต์ กดเพื่อไปดูแต่ละ flowchart:
calculate_total(prices) — the counter loop of find_total(scores). calculate_total(prices) — loop นับรอบของ find_total(scores)
calculate_average(prices) — same two steps as find_average(scores). calculate_average(prices) — สองขั้นตอนเดียวกับ find_average(scores)
find_most_expensive(prices) — the max scan of find_highest(scores). find_most_expensive(prices) — การไล่หาค่ามากของ find_highest(scores)
Read a password and set three flags with has_uppercase, has_lowercase, and has_digit. The password is strong only if it is longer than 8 characters and all three flags are true.
อ่านรหัสผ่านแล้วตั้งค่า flag สามตัวด้วย has_uppercase, has_lowercase และ has_digit รหัสผ่านจะ strong ก็ต่อเมื่อยาวเกิน 8 ตัวอักษร และ flag ทั้งสามเป็นจริง
Main program โปรแกรมหลัก
How the main flow works: page 1 reads the password and runs the three checks as predefined processes, then passes through connector 2. Page 2 is one combined decision — if the length test and all three flags hold, print “Strong password”; otherwise print “Weak password”. Both outputs merge at one connector, then Stop.
การทำงานของ flow หลัก: หน้า 1 อ่านรหัสผ่านและทำการตรวจสามอย่างเป็น predefined process แล้วผ่าน connector 2 หน้า 2 เป็น decision รวมหนึ่งครั้ง — ถ้าเงื่อนไขความยาวและ flag ทั้งสามเป็นจริง แสดง “Strong password” ไม่งั้นแสดง “Weak password” ทั้งสองผลลัพธ์มารวมกันที่ connector เดียวแล้วจึง Stop
Strong password
has_uppercase(password)
Walk the characters; the moment one passes char.isupper() the function returns True. If the loop finishes with no hit, it returns False.
ไล่ดูตัวอักษรทีละตัว ทันทีที่ตัวใดผ่าน char.isupper() ฟังก์ชันจะคืน True ถ้า loop จบโดยไม่เจอ จะคืน False
True
has_lowercase(password) / has_digit(password)
Exactly the shape of has_uppercase(password) — swap char.isupper() for char.islower() or char.isdigit(). รูปเดียวกับ has_uppercase(password) — เปลี่ยน char.isupper() เป็น char.islower() หรือ char.isdigit()
True
True
References อ้างอิง
- Flowchart — Wikipedia Flowchart — Wikipedia
-
Python docs —
for/whilestatements (cards 10–11). Python docs — คำสั่งfor/while(card 10–11) - G3 Computing Textbook §4.14 Control Flow (flowchart symbols and structured programming). G3 Computing Textbook §4.14 Control Flow (สัญลักษณ์ flowchart และ structured programming)