Programming Fundamentals and Logic
What the exam tests
Algorithms and flowcharts, the three control structures, operators, loops, arrays and functions, and dry-running short code to state the output.
Core concepts
- Flowchart symbols: oval for start and stop, parallelogram for input and output, rectangle for process, diamond for decision, arrow for flow.
- Control structures: sequence, selection (if/else, switch) and iteration (for, while, do-while). Use for when the count is known, while when the condition must be tested first, and do-while when the body must run at least once.
- Operators: arithmetic, relational (==, !=, <, >), logical (AND, OR, NOT). Integer division discards the remainder; modulus % returns it.
- Arrays store many values of one type under one name, usually indexed from 0.
- Errors: syntax (will not compile), logical (runs but gives a wrong answer), runtime (crashes, for example division by zero).
Worked example
sum โ 0; for i = 1 to 4: sum โ sum + i*i. Trace it. At i = 1, sum = 0 + 1 = 1. At i = 2, sum = 1 + 4 = 5. At i = 3, sum = 5 + 9 = 14. At i = 4, sum = 14 + 16 = 30. The loop then sets i = 5, which fails the test, so it stops. Output is 30, and the body ran four times, not five.
Traps
- Confusing = (assignment) with == (comparison).
- Off-by-one loop bounds: 1 to n runs n times, 1 to n-1 runs n-1 times.
- Forgetting to initialise an accumulator to 0, or a product to 1.
- A while loop whose condition never changes runs forever and never reaches the output statement.