Programming techniques — OCR A-Level Computer Science
Programming techniques explained
Programming techniques covers 6 specification points, including Sequence, iteration, branching.
Read the full explanation
and (b) Recursion, how it can be used and compares…. Open a point below for its explanation and revision guidance.
(a) Programming constructs: sequence, iteration, branching.
Programming constructs are the basic control structures that determine the order in which a program's statements run. Sequence means statements execute one after another, top to bottom, unless a construct changes that order. Iteration repeats a block of statements, either a fixed number of times or while a condition holds, as in a loop that totals a list of values. Branching, also called selection, chooses one path from a condition, such as an if statement that prints a message only when a password is wrong. Learn to recognise each in pseudocode or code, and to write a short fragment that combines all three, because most real algorithms need them together.
(b) Recursion, how it can be used and compares to an iterative approach.
Recursion is a technique in which a subroutine calls itself to solve a smaller version of the same problem. Every recursive solution needs a base case that stops the calls and a recursive case that moves towards it; for example, factorial(n) returns 1 when n is 0 and otherwise returns n × factorial(n − 1). It suits problems with a self-similar structure, such as tree traversal. An iterative approach repeats a block of code with a loop such as while or for, keeping loop variables in fixed storage. Recursion can give shorter, clearer code for naturally recursive problems, but each call adds a stack frame, so very deep recursion risks stack overflow. Learners should trace calls and state both cases.
(c) Global and local variables.
A variable's scope is the part of a program where it can be accessed. A global variable is declared outside any subroutine, so every subroutine can read it and, usually, change it. A local variable is declared inside a subroutine and exists only while that subroutine runs; other code cannot access it. Passing values as parameters keeps subroutines self-contained and easier to test. Overusing globals creates hidden dependencies, because any subroutine may change a shared value unexpectedly. If a local and a global share a name, the local takes precedence inside its subroutine. Learners should identify which variables each subroutine can read and write.
(d) Modularity, functions and procedures, parameter passing by value and by reference.
Modularity divides a program into smaller, self-contained modules, each performing one defined task, so large problems become manageable and parts can be reused. Modules are often functions, which return a value, or procedures, which carry out an action without returning one. Parameter passing controls how data moves between the caller and the subroutine. Pass by value copies the argument's value into the parameter, so changes made inside the subroutine leave the caller's variable unchanged. Pass by reference passes the variable's location, so the subroutine can alter the caller's copy. For example, if a subroutine doubles its parameter and the caller passes a variable holding 5, the caller's value stays at 5 under pass by value but becomes 10 under pass by reference. Learners should trace such short programs to predict their output.
(e) Use of an IDE to develop/debug a program.
An integrated development environment (IDE) combines a code editor, a translator such as a compiler or interpreter, and tools such as a debugger in one application. To develop a program, students use features such as syntax highlighting, auto-completion and immediate error flagging, which reduce typing and expose mistakes early. To debug a program, they use breakpoints to pause execution at a chosen line, step-through execution to run one line at a time, and watch windows to inspect variable values as the program runs. Practise setting a breakpoint, stepping through a short loop, and spotting the line where a variable first takes an unexpected value. An IDE locates errors quickly, but the programmer must still reason about the algorithm.
(f) Use of object oriented techniques.
Object-oriented techniques structure a program around classes and objects. A class is a blueprint that defines attributes (data) and methods (behaviour), and an object is an instance created from that class. For example, a Car class might define a speed attribute and an accelerate() method, and each car object holds its own speed value. Encapsulation hides data inside the class and changes it through methods; inheritance lets a subclass such as ElectricCar reuse and extend a superclass such as Car; polymorphism lets one method call produce different behaviour for different objects. To use these techniques, you design classes around the problem, create objects from them, and apply inheritance and encapsulation to reduce repetition and simplify maintenance.
Your focus
- Identify sequence, iteration and branching in a given pseudocode fragment.
- Write a short fragment that uses iteration and branching together.
- Explain the difference between a loop and a selection statement using an example.
Show all 19 objectives
- Trace the sequence of calls and returns produced by a short recursive function.
- Identify the base case and the recursive case in a given recursive subroutine.
- Compare recursive and iterative solutions to the same problem, giving one advantage and one disadvantage of each.
- Identify whether a given variable is global or local from a short code extract.
- Predict which subroutines can read or change a named variable in a small program.
- Explain the risk of using global variables for shared data in a program with several subroutines.
- Predict the output of a short program that uses pass by value.
- Predict the output of a short program that uses pass by reference.
- Distinguish a function from a procedure using whether a value is returned.
- Explain why dividing a program into modules aids testing and reuse.
- Identify features of an IDE that support developing a program.
- Use breakpoints and step-through execution to locate a logic error in a short program.
- Explain how watch windows help a programmer inspect variable values during debugging.
- Identify the attributes and methods defined in a class from a short program.
- Distinguish between a class and an object created from it.
- Select the object-oriented feature, such as inheritance or polymorphism, that matches a described design.
Programming techniques exam tips
Marking Points
- Defines sequence as statements executed one after another in the order written, from top to bottom.
- Defines iteration as a construct that repeats a block of statements while a condition holds or for a set number of times.
- Defines branching as a construct that selects one path from a condition and runs only that path.
- Gives a short pseudocode or code example for each construct that shows its control flow correctly.
- Distinguishes iteration from branching by stating that a loop returns to repeat code, whereas a selection runs a chosen path once.
- Defines recursion as a subroutine that calls itself on a smaller version of the same problem.
- Identifies a base case that stops the recursion and a recursive case that moves towards it.
- Gives a suitable use of recursion, such as tree traversal or factorial, and states the recurrence correctly.
- Compares recursion with iteration by contrasting the call stack with fixed loop variables.
- Notes that recursion can be clearer for naturally recursive problems but risks stack overflow when deep.
- Defines scope as the region of a program in which a variable can be accessed.
- Explains that a global variable is declared outside subroutines and can be accessed by all of them.
- Explains that a local variable exists only within the subroutine in which it is declared.
- Gives a reason for preferring local variables, such as avoiding unintended changes and supporting reuse.
- Identifies that a local variable with the same name as a global variable takes precedence inside its subroutine.
- Defines a module as a self-contained unit that performs one defined task.
- Explains that modularity makes programs easier to test, maintain and reuse.
- Distinguishes a function, which returns a value, from a procedure, which does not return one.
- Explains that pass by value copies the argument, so the caller's variable is unchanged.
- Explains that pass by reference passes the variable's location, so the caller's variable can be altered.
- Traces the output of a short program correctly under each parameter passing method.
- Identifies an IDE as one environment that combines an editor, a translator and debugging tools.
- Describes a development feature, such as syntax highlighting or error flagging, and explains how it helps.
- Describes breakpoints as pausing execution at a chosen line.
- Describes step-through execution as running one line at a time to observe behaviour.
- Uses watch windows to inspect variable values at a stated point in execution.
- Identifies a class as a template that defines attributes and methods.
- Distinguishes an object as an instance of a class that holds its own attribute values.
- Explains encapsulation as keeping data inside a class and accessing it through methods.
- Applies inheritance so a subclass reuses and extends the attributes and methods of a superclass.
- Describes polymorphism as one method call producing different behaviour depending on the object.
Examiner Tips
- 💡Name each construct explicitly and give a short example, so the examiner can see that you understand its control flow.
- 💡When writing a loop, check that its condition can change, so that the loop cannot run forever.
- 💡Keep examples short and focused on the construct being illustrated rather than on unrelated program details.
- 💡Trace factorial(3) on paper, writing each call and each return value in order, so you can recognise the pattern when it appears in an option.
- 💡Eliminate any option that describes recursion without a stopping condition before comparing the remaining options.
- 💡When an option compares the two approaches, check that it names a genuine trade-off such as stack use or clarity rather than a vague general claim.
- 💡List, for each subroutine in a question, the variables it declares and the variables it can see from outside it.
- 💡Check whether an option describes access rather than just storage when it discusses where a variable can be used.
- 💡When a question involves a shadowed name, decide which variable the subroutine actually uses before choosing an answer.
- 💡Trace a short program line by line, writing down the value of each variable after every step, before choosing an answer.
- 💡When a question asks about a subroutine's effect on the caller, check which passing method is stated and decide whether the caller's variable can change.
- 💡Eliminate options that describe a procedure as returning a value, because this is a common distractor.
- 💡For a debugging question, identify the first line where the variable's value becomes wrong, rather than the line where the program finally crashes.
- 💡When asked how an IDE helps, name a specific feature and state the benefit it gives the programmer.
- 💡Practise by stepping through a short loop by hand and comparing each variable value with the watch window.
- 💡When an option uses two similar terms, check whether it describes the blueprint (class) or the instance (object).
- 💡Match each option's action verb, such as creates, inherits or overrides, to the concept it describes.
- 💡Eliminate options describing shared global variables or single-procedure code, since these are not object-oriented features.
Common Mistakes
- Describes branching as a loop that repeats code; correct by stating that branching chooses between paths and runs the chosen path once.
- Treats iteration as a single pass through code; correct by showing that the block repeats until the stated condition is false or the count is reached.
- Names sequence as an optional construct; correct by stating that every program relies on sequence as its default order of execution.
- Omits the base case, so the description suggests the calls never end; include a condition that stops the recursion.
- Treats recursion and iteration as interchangeable in every case; state that some problems suit one approach better than the other.
- Describes recursion only as a loop that repeats; explain that each call is a separate invocation with its own variables held on the call stack.
- Claims a local variable can be read by other subroutines; state that only the declaring subroutine can access it unless its value is passed as a parameter.
- Assumes global variables are always the better choice because they are easy to reach; explain the hidden dependencies they create.
- Confuses scope with lifetime; explain that scope defines where code can access a variable, while lifetime defines how long it exists in memory.
- Claims that pass by value changes the caller's variable; correct this by stating that only the copy inside the subroutine changes.
- Describes a procedure as returning a value; correct this by stating that a function returns a value and a procedure does not.
- Treats modularity as simply splitting code into lines; correct this by specifying self-contained modules that each perform one defined task.
- Assumes pass by reference copies the value; correct this by stating that the subroutine receives the variable's location, so changes affect the original.
- Treats the IDE as a substitute for understanding the algorithm; correct this by noting that the IDE locates errors but the programmer must still reason about the logic.
- Describes a breakpoint as deleting a line of code; correct this by stating that it pauses execution at a chosen line.
- Confuses debugging with simply compiling the code; correct this by stating that debugging examines the running program to find where the logic goes wrong.
- Lists features without linking them to a purpose; correct this by stating what each feature helps the programmer do.
- Treats a class and an object as the same thing; the class is the blueprint and the object is created from it.
- Describes inheritance as copying code by hand into each class; a subclass automatically receives the superclass's attributes and methods.
- Lists OO terms without showing how a class design solves the problem; link each feature to a specific class or object in the solution.