Algorithm design II: pseudocode and flowcharts
AdvancedPseudocode's three structures: sequence, IF-THEN-ELSE, WHILE/FOR; flowchart symbols: rounded rectangle = start/end, parallelogram = input/output, rectangle = process, diamond = decision (one in, two out).
- Three ways to express an algorithm: words, pseudocode, flowchart.
- Pseudocode's three structures: sequence, IF-THEN-ELSE, FOR/WHILE loops.
- Symbols: rounded rectangle = start/end, parallelogram = input/output, rectangle = process, diamond = decision.
文體小知識:原理講解課先講清楚概念和規則,再用實例演示,令抽象的設計方法變得具體易明。
§Text Passage
1An algorithm is a clear sequence of steps that solves a problem. In S1 you wrote steps in words; in S2 you learn two more professional ways to express them: pseudocode and flowcharts. Neither is a real program — they are the design drawings you make before coding.
2Pseudocode describes logic in English-like structure with three building blocks: SEQUENCE (step by step), IF-THEN-ELSE (choosing between two paths) and WHILE/FOR loops (repetition). For example, 'if the score is at least 50 show Pass, otherwise show Fail' becomes: IF score ≥ 50 THEN OUTPUT 'Pass' ELSE OUTPUT 'Fail' — clear logic that translates easily into any programming language later.
3A flowchart draws the steps with standard symbols: rounded rectangles for Start/End, parallelograms for input/output, rectangles for processing (calculation), diamonds for decisions, and arrows for flow direction. A decision diamond always has one way in and two ways out (Yes/No) — a favourite exam point.
4Loops appear in flowcharts as a 'return arrow': to input the scores of 30 students one by one, each score is added to a total, then a diamond asks 'Have 30 been entered?' If not, the arrow leads back to keep inputting. Drawing the flowchart first saves a lot of rework when coding.
1Summary 重點摘要
Pseudocode's three structures: sequence, IF-THEN-ELSE, WHILE/FOR; flowchart symbols: rounded rectangle = start/end, parallelogram = input/output, rectangle = process, diamond = decision (one in, two out).
2Vocabulary cards Vocabulary
A clear, finite, executable sequence of steps that solves a problem.
A way of describing program logic in English-like structure; not an executable program.
A diagram showing algorithm steps with standard symbols and arrows.
The rounded rectangle marking Start and End of a flowchart.
The flowchart symbol for input and output.
The rectangle for a calculation or processing step, e.g. sum ← a + b.
The diamond for a decision; always one way in and two ways out (Yes/No).
A structure that repeats steps, shown in a flowchart by a return arrow.
Putting a value into a variable, often written ← in pseudocode, e.g. count ← count + 1.
3Step by step Steps
- Three ways to express one algorithmOne algorithm can be expressed three ways: words, pseudocode and a flowchart. Take 'average of three marks': in words it takes three or four sentences, but anyone can read it; in pseudocode three lines — input the three marks, set average to the sum of the three divided by three, then output average; in a flowchart, four or five symbols say it all. All three express the same algorithm; exams ask for a specific form, and you must be able to translate between the three.
- Basic pseudocodePseudocode describes logic in English-like structure. The basic frame: after BEGIN, INPUT a, b, then sum ← a + b, OUTPUT sum, and finally END. The arrow ← means 'store the right-hand value into the left-hand variable', and the order is compute the right side first, then store it on the left — sum ← a + b calculates a + b before storing into sum. This symbol differs from the maths equals sign because it has a direction; a favourite exam question asks what sum ← sum + 1 means, and the answer is: take the old value of sum, add one, and store it back into sum.
- IF-THEN-ELSE decisionsThe decision structure is written IF condition THEN action ELSE other action. For a pass check: IF mark ≥ 50 THEN OUTPUT 'Pass' ELSE OUTPUT 'Fail'. The two paths must cover every case: 50 or above goes to THEN, while 49.5, 49 and even 0 all go to ELSE — no mark may fall outside both. Watch the boundary when writing the condition: '50 or above passes' means mark ≥ 50, not mark > 50; dropping the equals sign wrongly fails every student scoring exactly 50.
- WHILE loopsA WHILE loop suits repetition where only a condition is known and the count is not. For example, WHILE count < 30 DO (INPUT score; count ← count + 1): as long as count stays below 30, the condition holds and the program keeps inputting scores; when count reaches 30, the condition fails and the loop ends. The key is a statement inside the loop that changes the condition — here count ← count + 1; omit it and count never changes, so the loop never stops. Exams often ask how many times a loop runs; trace it circle by circle with real numbers.
- FOR loopsWhen the number of repeats is known, use a FOR loop: FOR i FROM 1 TO 10 DO OUTPUT i — i takes 1 to 10 in turn and the program prints 1 to 10. The rule for choosing: a known count (print ten numbers, process 30 students) takes FOR; a known condition only (until the answer is right, until the total passes 100) takes WHILE. Either can be rewritten as the other, but exams expect the more suitable one by the wording of the question, with the loop variable's start, end and step written out.
- Matching symbols to stepsMatch the five flowchart symbols to their jobs: 'input two numbers' takes a parallelogram; 'add the two numbers' takes a rectangle; 'is it greater than 0?' takes a diamond; Start and End take rounded rectangles; arrows connect every step, flowing top to bottom. The mnemonic: asking takes a diamond, acting takes a rectangle, data in and out takes a parallelogram. Common errors: drawing input or output as a rectangle, or giving a decision only one exit; after drawing, check each symbol's shape against its purpose one by one.
- Drawing a loopA loop appears in a flowchart as a 'return arrow'. To input 5 scores and total them: after the input step (parallelogram), a rectangle adds the score to the total; the diamond asks '5 done?' — if no, the arrow returns to the input step; if yes, the arrow goes on to output the total. That return arrow is the signature of a loop. Before drawing, settle what the loop variable is, when it increases and when the check happens; only then will the chart come out right.
4Diagrams Diagrams
5Worked example Example
Example: write pseudocode for 'input two numbers and output the larger one', and name the flowchart symbols needed.
- Build the frame first: after BEGIN, INPUT num1, num2 — a parallelogram in the flowchart, because data enters from outside.
- Write the decision in the middle: IF num1 > num2 THEN OUTPUT num1 ELSE OUTPUT num2 — a diamond in the flowchart, its two exits labelled yes and no.
- End with END, drawn as a rounded rectangle; the two output lines are also parallelograms.
- Check the boundary: when num1 = num2 the condition num1 > num2 fails, the ELSE path outputs num2, and the result is still correct — with equal numbers either output is right.
- Answer: Pseudocode: BEGIN; INPUT num1, num2; IF num1 > num2 THEN OUTPUT num1 ELSE OUTPUT num2; END. Symbols needed: rounded rectangle (start/end), parallelogram (input/output), diamond (decision), rectangle (process), arrows (flow). With equal numbers the ELSE path runs and the output is still correct.
6Common mistakes Mistakes
7Quick check Quick check
Q1What does a diamond represent in a flowchart? A. Process B. Decision C. Input
Q2Which symbol is used for 'input a number' in a flowchart?
Q3Write IF-THEN-ELSE pseudocode for 'if the temperature is above 30 degrees turn on the air conditioning, otherwise do not'.
Q4An action must repeat exactly 10 times — WHILE or FOR, and why?
Q5How is a loop shown in a flowchart?
8Recap Recap
- Three ways to express an algorithm: words, pseudocode, flowchart.
- Pseudocode's three structures: sequence, IF-THEN-ELSE, FOR/WHILE loops.
- Symbols: rounded rectangle = start/end, parallelogram = input/output, rectangle = process, diamond = decision.
- Decisions need one in, two out; loops are drawn as return arrows.
- Known count → FOR; condition only → WHILE.