Notations, stepwise refinement and logic
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.
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 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.
What is the precedence of the logic operators, highest first?
NOT binds tightest, then AND, then OR — use brackets when in doubt.
By De Morgan's law, NOT (A AND B) is equivalent to:
NOT (A AND B) = (NOT A) OR (NOT B); similarly 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)