Basic Programming Concepts: Algorithms, Control and Debugging
Basic programming concepts tests whether you can read and write algorithms, trace their execution, choose data types and control structures, and identify errors. Answers should be language-neutral pseudocode unless a language is specified.
Building blocks
A variable is a named storage location whose value can change; a constant is fixed during execution. Common data types are integer, real, Boolean, character and string. Assignment stores a value; comparison tests equality. Arithmetic operators include +, -, *, /, DIV and MOD. Relational operators include =, <>, <, >, <= and >=; Boolean operators are AND, OR and NOT.
Control structures
Sequence executes statements in order. Selection uses IF…THEN…ELSE or CASE. Iteration is count-controlled (FOR) or condition-controlled (WHILE, REPEAT…UNTIL). A FOR loop from m to n runs n − m + 1 times when the step is 1. Functions return a value; procedures do not necessarily. Parameters pass data; local variables exist only inside a subprogram, while global variables are visible more widely. Arrays store multiple elements under one name and are indexed from 0 or 1 depending on the language.
Worked example
total ← 0
FOR i ← 1 TO 5
IF i MOD 2 = 0 THEN
total ← total + i
ENDIF
NEXT i
OUTPUT total
Trace: i = 1, odd, skip; i = 2, total = 2; i = 3, skip; i = 4, total = 6; i = 5, skip. Output is 6, the sum of even numbers from 1 to 5. If the condition were i MOD 2 = 1, the output would be 9.
Traps
Do not confuse assignment with equality, or integer division with MOD. Check loop bounds and whether the loop runs n or n + 1 times. Initialise accumulators before use. Watch operator precedence. A syntax error prevents execution; a logic error runs but gives wrong output; a runtime error stops execution. Test normal, boundary and invalid data.