Algorithms and pseudocode
Algorithms and pseudocode
- An algorithm is a solution expressed as a sequence of defined steps.
- We plan the data in an identifier table, then write it in pseudocode.
- Three building blocks cover every algorithm.
What makes a good algorithm
- Each step is unambiguous (one meaning), deterministic (same input → same output), finite (the steps end), and effective (each can be done).
- First, list every piece of data in an identifier table — name, data type, description:
| Variable | Type | Description |
|---|---|---|
ItemCost |
REAL |
cost of the item |
InStock |
BOOLEAN |
TRUE if in stock |
- Use descriptive names (
ItemCost, notx). Common types:INTEGER,REAL,STRING,CHAR,BOOLEAN,DATE.
An algorithm is "deterministic". This means:
Deterministic = same input → same output every time. (Finite = the steps end; unambiguous = one meaning per step.)
An identifier table lists, for each piece of data, its:
The identifier table records each variable's name, type and description before you write the algorithm.
The three constructs
# 1. Sequence — steps in order
INPUT Name
OUTPUT "Hello", Name
# 2. Selection — a choice
IF Age >= 18 THEN
OUTPUT "Adult"
ELSE
OUTPUT "Minor"
ENDIF
# 3. Iteration — a loop
FOR i ← 1 TO 10
OUTPUT i
NEXT i
- A WHILE loop tests before each pass (may run zero times); a REPEAT...UNTIL tests after (always runs at least once).
How does a WHILE loop differ from a REPEAT...UNTIL loop?
WHILE checks first (can run 0 times); REPEAT...UNTIL checks after, so it always runs at least once.
Common operations
- Assignment uses an arrow:
x ← 5(=is for comparison). - Arithmetic
+ - * /, plusDIV(integer division) andMOD(remainder) — e.g.17 MOD 5 = 2. - Comparisons
= <> < > <= >=; logicAND OR NOT; strings join with&. - Every program follows input → process → output.
In this pseudocode, which symbol means assignment (store a value)?
Assignment uses ← (e.g. x ← 5); = is reserved for comparison.
What is the value of 17 MOD 5?
MOD gives the remainder: $17 = 3 \times 5 + 2$, so 17 MOD 5 = 2. (17 DIV 5 = 3.)
Most programs follow which overall shape?
Read the inputs, process them, then produce the output — listing inputs/outputs first keeps the algorithm clean.
You've got it
- an algorithm's steps are unambiguous, deterministic, finite, effective
- plan data in an identifier table (name, type, description)
- three constructs: sequence, selection (IF/CASE), iteration (FOR/WHILE/REPEAT)
- WHILE tests before (0+ times), REPEAT tests after (1+ times); assignment is
←