2.2.1. Programming Techniques
2.2.1. Programming Techniques
www.pmt.education
Specification:
2.2.1 a)
● Programming constructs
○ Sequence
○ Iteration
○ Branching
2.2.1 b)
● Recursion
○ How it can be used
○ How it compares to an iterative approach
2.2.1 c)
● Global and local variables
2.2.1 d)
● Modularity, functions and procedures
○ Parameter passing by value
○ Parameter passing by reference
2.2.1 e)
● Use of an IDE to develop / debug a program
2.2.1 f)
● Use of object-oriented techniques
www.pmt.education
Programming Constructs
- Sequence
Code is executed line-by-line, from top to bottom.
- Branching
A certain block of code is run if a specific condition is met, using IF
statements. This is also known as ‘selection’.
- Iteration
A block of code is executed a certain number of times or while a condition is
met. Iteration uses FOR, WHILE or REPEAT UNTIL loops.
Iteration can be either:
- Count-controlled
- Condition-controlled
www.pmt.education
Recursion
function factorial(number)
if number == 0 or 1:
return 1
else:
return number * factorial(number - 1);
endif
end function
There are some disadvantages to recursion, the biggest being its inefficient use of
memory. If the subroutine calls itself too many times, there is a danger of a stack overflow,
which is when the call stack runs out of memory. This would cause the program to crash.
Tail recursion is a form of recursion which is implemented in a more efficient way in which
less stack space is required. Another problem with recursion is that it is difficult to trace,
especially with more and more function calls.
www.pmt.education
Global and Local Variables
Variables can be defined with either global or local scope. Scope refers to the section of
code in which the variable is available.
Global variables, on the other hand, can be accessed across the whole program. All
variables used in the main body of a program are automatically declared to be global.
These are useful for values that need to be used by multiple parts of the program. On the
whole, however, using global variables is not recommended because they can be
unintentionally overwritten and edited. As global variables are not deleted until the
program terminates, they require more memory than local variables which are deleted
once the subroutine has been completed.
In the event that a local variable exists within a subroutine with the same name as a global
variable, the local variable will take precedence.
www.pmt.education
This process is also known as stepwise refinement. These modules form blocks of code
called subroutines, which can be categorised as either functions or procedures. Below is
an example of a top-down design for a problem involving a book reservation system:
In exam questions, you should assume parameters are passed by value unless you are
told otherwise. The following format will be used:
www.pmt.education
Use of an IDE
- Stepping
This allows you to monitor the effect of each individual line of code by
executing a single line at a time.
- Variable watch
Sometimes used to pinpoint errors, this is a useful feature to observe how
the contents of a variable change in real-time through the execution of a
program.
- Breakpoint
IDEs allow users to set a point in the program at which the program will stop.
This can either be based on a condition or set to occur at a specific line. This
can help to pinpoint where an error is occurring.
- Debugging tools
Some IDEs also provide run-time detection of errors with a guide as to where
in the code they are likely to have occurred through line numbers and
highlighting.
www.pmt.education
Use of object-oriented techniques
Object-oriented languages are built around the idea of classes. A class is a template for an
object and defines the state and behaviour of an object. State is given by attributes which
give an object’s properties. Behaviour is defined by the methods associated with a class,
which describe the actions it can perform.
Classes can be used to create objects by a process called instantiation. An object is a
particular instance of a class, and a class can be used to create multiple objects with the
same set of attributes and methods.
www.pmt.education