Notations, stepwise refinement and logic
| English | Chinese | Pinyin |
|---|---|---|
| stepwise refinement | 逐步求精 | zhú bù qiú jīng |
| structured English | 结构化英语 | jié gòu huà yīng yǔ |
| pseudocode | 伪代码 | wěi dài mǎ |
| flowchart | 流程图 | liú chéng tú |
| precedence | 优先级 | yōu xiān jí |
| De Morgan's law | 德摩根定律 | dé mó gēn dìng lǜ |
Three ways to write an algorithm
- The same algorithm can be written three ways — and you must convert between them.
- We design top-down with stepwise refinement 逐步求精.
- And we control the flow with logic statements.
Three notations
- Structured English 结构化英语 — natural language with indentation and fixed keywords; good for a high-level view.
- Pseudocode 伪代码 — the keyword notation (IF/WHILE/FOR…); closest to real code.
- Flowchart 流程图 — a diagram with standard shapes:
| Shape | Meaning |
|---|---|
| Rounded rectangle | Start / Stop |
| Parallelogram | Input / Output |
| Rectangle | Process |
| Diamond | Decision |
| Arrow | Flow of control |
Each IF becomes a decision diamond; each loop is a back-arrow.

A flowchart for averaging a list of numbers, using the standard shapes
In a flowchart, which shape represents a decision?
A diamond is a decision; rounded rectangle = start/stop, parallelogram = input/output, rectangle = process.
Match each flowchart shape to its meaning.
Parallelogram = I/O, rectangle = process, rounded rectangle = start/stop, diamond = decision.
Stepwise refinement
- Start with a high-level outline, then expand each step until it is small enough to code.
- Level 1:
Read the numbers→Compute the average→Output the average. - Level 2 expands "compute the average" into a
FORloop that sums the values then divides byn. - Each refinement keeps the previous structure and adds detail.

Stepwise refinement expands each high-level step into more detailed sub-steps until it can be coded
Stepwise refinement: outline to code
Step down the levels. You start with the whole task in one line and keep expanding each step into smaller ones — until every step is simple enough to code directly.
Stepwise refinement is the technique of:
You refine a high-level outline level by level, adding detail while keeping the structure.
Logic statements
- A logic statement is a Boolean condition controlling
IF,WHILEorREPEAT— built from comparisons andAND/OR/NOT. - Precedence 优先级 (highest first): NOT, then AND, then OR — use brackets when unsure.
- Watch out:
a = 1 OR 2is wrong — writea = 1 OR a = 2. - De Morgan's law 德摩根定律:
NOT (A AND B)is the same as(NOT A) OR (NOT B)— handy for simplifying conditions.
Put the logic operators in order of precedence, highest (evaluated first) to lowest.
NOT binds tightest, then AND, then OR — use brackets when in doubt.
By De Morgan's law, NOT (A AND B) is the same as (NOT A) OR (NOT B).
NOT distributes over the bracket and flips AND↔OR; likewise NOT (A OR B) = (NOT A) AND (NOT B).
To test whether a is 1 or 2, the correct condition is:
Each side of OR must be a full comparison: a = 1 OR a = 2. Writing a = 1 OR 2 is a common error.
You've got it
- three notations: structured English, pseudocode, flowchart (diamond = decision)
- stepwise refinement expands a high-level outline step by step
- logic precedence: NOT → AND → OR; use brackets when unsure
- De Morgan:
NOT (A AND B)=(NOT A) OR (NOT B)