What Algorithms Are — Problems, Models & Correctness

reading pseudocode

Pseudocode is how we write down an algorithm for humans, not for a compiler. It is a halfway language: more precise than a paragraph of prose, but freed from the punctuation and ceremony of a real programming language. The point is to show the method clearly so a reader can understand and reason about it, without getting tangled in whether a semicolon is missing. If real code is a legal contract, pseudocode is the plain-English summary that captures every important clause.

There are no strict rules, but conventions are widely shared and easy to read. Assignment is written with a left arrow or equals, as in x = x + 1 ("replace x by its old value plus one"). Indentation shows which statements belong to a loop or an if. "for i = 1 to n" repeats with i taking 1, 2, up to n; "while condition" repeats as long as the condition holds; "if ... then ... else ..." chooses a branch. Array elements are written A[i], and we often start indices at 1. Here is a full example reading: to find a maximum, set m = A[1]; then for i = 2 to n, if A[i] > m then m = A[i]; finally return m. Reading it line by line, you can trace exactly what happens to m on any input and convince yourself it ends up holding the largest element.

Pseudocode deliberately leaves out the parts that do not affect the method: which exact data type holds a number, how memory is allocated, how errors are reported. This is a feature, not a sloppiness — it keeps the idea portable across languages and lets you analyse cost (count the loop iterations, the comparisons) without distraction. When you read pseudocode, the goal is not to run it in your head like a machine, but to understand why it works: what each loop maintains, why it is correct when it stops, and roughly how much work it does.

MAX(A, n): m = A[1]; for i = 2 to n: if A[i] > m then m = A[i]; return m. On A = [4,1,7,3] the variable m takes 4, then 7, and 7 is returned.

Trace the variables by hand; that is how you read pseudocode.

Watch the indexing convention. Many textbooks number arrays from 1; most real languages number from 0. The same pseudocode can be off by one when translated if you ignore this.

Also called
pseudocode虛擬碼偽代碼