Before the questions, make sure you can: state the properties an algorithm must have; read and write pseudocode using the conventions of this course; name every flowchart symbol and its meaning; build a trace table for a fragment containing a loop and predict its output; name the loop-termination techniques and say when each is appropriate; and spot an infinite loop or an off-by-one error by inspection.
What an algorithm is
An algorithm is a finite sequence of unambiguous, executable steps that solves a problem for all valid inputs and terminates. Four properties are examinable: it must be finite (it stops), definite (each step has exactly one meaning), effective (each step can actually be carried out), and it must produce output from zero or more inputs. An algorithm is language-independent; pseudocode and flowcharts are two notations for writing one down before committing to Java syntax.
Pseudocode conventions
declare count, total, average // introduce variables
count <- 0 // assignment: evaluate right side, then store
print "Enter a mark: " // output
input mark // read a value from the user
if mark >= 50 then
print "Pass"
else
print "Fail"
endif
while count < 5 do
total <- total + mark
count <- count + 1
endwhile
for i <- 1 to 10 do
print i
endfor
Indentation shows nesting, and every if, while and for is closed explicitly.
<- means assign; = means compare. Confusing the two is the most
common pseudocode error.
Flowchart symbols
| Symbol | Name | Meaning |
|---|---|---|
| Oval / rounded box | terminal | start or end of the algorithm |
| Parallelogram | input/output | read a value, print a value |
| Rectangle | process | computation or assignment |
| Diamond | decision | a condition with two outgoing branches, yes and no |
| Small circle | connector | the chart continues elsewhere |
| Arrow | flow line | the order in which steps are performed |
| Double-sided rectangle | predefined process | a call to a separate module or method |
A loop appears in a flowchart as a flow line that goes backwards to a point above a decision diamond.
The three control structures
Every algorithm is built from only three structures: sequence (one step after another),
selection (if, if/else, nested if) and repetition (while,
for). Learning these three well is worth more than memorising syntax.
Assignment and tracing
Assignment is not an equation. In count <- count + 1 the right-hand side is evaluated with
the old value and the result replaces it. Swapping two variables therefore needs a third:
temp <- a
a <- b
b <- temp
A trace table (desk check) has one column per variable and one row per iteration:
sum <- 0
i <- 1
while i <= 4 do
sum <- sum + i
i <- i + 1
endwhile
print sum, i
| iteration | test on i |
sum after |
i after |
|---|---|---|---|
| 1 | 1 ≤ 4 true | 1 | 2 |
| 2 | 2 ≤ 4 true | 3 | 3 |
| 3 | 3 ≤ 4 true | 6 | 4 |
| 4 | 4 ≤ 4 true | 10 | 5 |
| 5 | 5 ≤ 4 false | — | — |
Output: 10 5. A counter always ends one step past the last value that passed the
test. Exam questions exploit this constantly.
Loop termination techniques
- Counter-controlled. The number of repetitions is known in advance. Natural fit
for a
forloop. - Sentinel-controlled. Repeat until a special “impossible” value arrives (read marks until −1). The sentinel must not be a legal data value and must never be processed.
- Flag-controlled. A boolean such as
foundorvalidis set inside the loop and tested by the loop condition. - User-query. After each pass the program asks Again? (y/n).
- End-of-file controlled. When reading a file, repeat while data remain.
Nested loops and top-down design
The inner loop runs completely for each pass of the outer loop, so an outer loop of m passes containing an inner loop of n passes executes the inner body m × n times. Reset any inner counter at the top of the outer body, not before the outer loop.
Top-down design (stepwise refinement) means solving the problem at a coarse level first — read data, compute statistics, print report — and refining each step until it is small enough to code directly. Its parts become methods later.
- Diamond = decision, parallelogram = input/output, rectangle = process, oval = start/end.
- After a counting loop the counter holds the first value that failed the test.
- A sentinel must lie outside the range of legal data and is never processed.
- An inner loop body runs m × n times inside an outer loop of m passes.
- Any variable the loop condition depends on must change inside the loop.
- Forgetting to initialise an accumulator before the loop.
- Updating the counter outside the loop body: an infinite loop.
- Using
<where<=was meant: the classic off-by-one error. - Processing the sentinel as data, which corrupts sums and averages.
Ready? Close the notes and practise.
39 questions. Predict the output before you check — that is the skill the exam measures.