1.1. Programming
1.1. Programming
4.1.1 Programming
Advanced Notes
www.pmt.education
Specification:
www.pmt.education
4.1.1.3 Arithmetic operations
Be familiar with and be able to use:
● addition
● subtraction
● multiplication
● real/float division
● integer division, including remainders
● exponentiation
● rounding
● truncation
www.pmt.education
4.1.1.7 String-handling operations in a programming language
Be familiar with and be able to use:
● length
● position
● substring
● concatenation
● character → character code
● character code → character
● string conversion operations
www.pmt.education
4.1.1.13 Local variables in subroutines
Know that subroutines may declare their own variables, called local
variables, and that local variables:
● exist only while the subroutine is executing
● are accessible only within the subroutine
Be able to use local variables and explain why it is good practice to do
so.
www.pmt.education
Data Types
The way in which data is stored depends on what the data is. A data type is defined by the
values it can take or the operations which can be performed on it.
In some situations, it might be possible to store one piece of data using various different
data types. In this case, the programmer must decide which option is the best suited to
solving a particular problem or which is the most memory-efficient.
For example, if a programmer needs to store a user’s age in years, they could use a string
or an integer. In this situation, using an integer would be the best option, because a
person’s age is only ever going to contain numerical digits.
www.pmt.education
User-defined data types
The way in which you use user-defined data types differs between programming
languages. It’s important that you know how to use them in your chosen language.
Programming Concepts
Programming languages support a variety of different statement types, some of which are
explained in the table below.
Variable declaration Creating a variable for the first time, giving it a name
and sometimes a data type. This allocates a portion of
the computer’s memory to the variable.
www.pmt.education
Definite and indefinite iteration
Iteration is the process of repeating a block of code. Examples of iteration include for
loops and whileloops.
Definite iteration is a type of iteration in which the number of repetitions required is known
before the loop starts.
In contrast to definite iteration, indefinite iteration is used when the number of repetitions
required is not known before the loop starts.
Nested Structures
Selection structures and iteration structures can be nested.
www.pmt.education
Meaningful Identifier Names
When declaring a constant, variable or subroutine, it’s
important to give it a sensible and meaningful identifier name.
This makes it easier for others to understand what the
purpose of the named object is within the program.
Addition When two values are added, the result is 128 + 42 = 170
the sum of the two values.
www.pmt.education
Relational Operations
You can make use of relational operators whenever you need to compare two values.
They are used in iterative and selection structures as well as for base cases in recursion.
Operation Example
Equal to 12 = 12
Boolean Operations
As explained earlier in this document, a Boolean data type is one whose value can only
ever be true or false. There are a series of operations that can be performed on Boolean
values.
www.pmt.education
Constants and Variables
When a program needs to store data, it usually does so using one of two types of data
item: constants or variables.
As their name suggests, variables can change their value during the execution of a
program, whereas a constant’s value cannot change once assigned.
HoursWorked ← USERINPUT HourlyRate ← 14
PAY ← 14 * HoursWorked HoursWorked ← USERINPUT
OUTPUT PAY PAY ← HourlyRate * HoursWorked
OUTPUT PAY
The pseudocode examples above show two different approaches to the same problem.
One approach uses hard-coded values whereas the other uses constants.
The code which makes use of constants is easier to understand as it clearly specifies that
14refers to an hourly rate. In the example which uses hard-coded values, it’s difficult to
understand why HoursWorkedis being multiplied by 14.
www.pmt.education
String-handling operations
Function Description
www.pmt.education
Random number generation
It’s important that you make yourself familiar with random number generation in your
chosen programming language.
Exception handling
When an error occurs in program code, an “exception” is said to be thrown. This could be
caused by using the wrong data type, attempting to divide by zero or attempting to access
a non-existent element in an array to name a few examples.
This code will prevent the program from crashing and might inform the user that an error
has occurred. Once the exception has been handled, the program uses the system stack
to restore its previous state before resuming execution.
Subroutines
www.pmt.education
Parameters of subroutines
Parameters are used to pass data between subroutines within programs. Specified within
brackets after a subroutine call, parameters hold pieces of information that the subroutine
requires to run.
Length ← USERINPUT
Width ← USERINPUT
OUTPUT CalculateArea(Length, Width)
SUBROUTINE CalcualteArea(x, y)
RETURN x * y
ENDSUBROUTINE
The subroutine CalculareAreain the pseudocode above takes two parameters,
Lengthand Width . It then returns the product of the two values.
The actual value passed by a parameter is called an argument. If a rectangle with sides of
height 4 and width 6 was input into CalculateArea , the parameters Lengthand Width
would have arguments 4 and 6 respectively.
A subroutine can return a value. One that always returns a value is called a function, but
don’t think that procedures can’t return a value, they can (but don’t always).
Subroutines that return values can appear in expressions and be assigned to a variable or
parameter.
Length ← USERINPUT
Width ← USERINPUT
Area ← CalculateArea(Length, Width)
OUTPUT Area
SUBROUTINE CalcualteArea(x, y)
RETURN x * y
ENDSUBROUTINE
For example, in the pseudocode above, the variable Areais assigned to the subroutine
CalculateArea . The value taken by the variable will be the value returned by the
subroutine.
www.pmt.education
Local variables in subroutines
A local variable is a variable that can only be accessed from the subroutine within which it
is declared. They only exist in the computer’s memory when their parent subroutine is
executing. This makes local variables a more memory efficient way of storing data than
using global variables, which are discussed below.
Global variables
In contrast to local variables, global variables can be accessed from any part of a program
and exist in memory for the entire duration of the program’s execution.
Local variables can be given the same identifier name as global variables, although this is
generally considered bad practice. When the local variable’s value is changed, the global
variable’s value remains the same.
Stack frames are used by computers to store return addresses, parameters and local
variables for each subroutine call that occurs during the execution of a program.
If one subroutine calls another, nesting is said to occur. Each subroutine call will be
pushed onto the computer’s call stack in the form of a stack frame before the subroutine’s
code begins to execute. When the nested subroutine finishes executing, the stack frame is
popped from the call stack and the computer uses the information to return to execution of
the previous subroutine.
1 Name ← USERINPUT
2 OUTPUT Greeting(Name)
3
4 SUBROUTINE Greeting(Name)
5 TimeOfDay ← GetTimeOfDay()
6 RETURN “Good “ + TimeOfDay + Name
7 ENDSUBROUTINE
8
9 SUBROUTINE GetTimeOfDay()
10 IF Time < 12:00 THEN
11 RETURN “morning”
12 ELSE
13 RETURN “afternoon”
14 ENDIF
15 ENDSUBROUTINE
www.pmt.education
Stack Frame Example
When the pseudocode above is run, the subroutine GetTimeOfDayis called from within
the subroutine Greetingand nesting occurs.
The first stack frame to be pushed onto the call stack is for the subroutine Greeting
:
Call Stack
Subroutine Return Parameters Local
Name Address Variables
When the subroutine GetTimeOfDayis called, another stack frame is pushed onto the
call stack and is placed on top of the frame representing Greeting:
Call Stack
Subroutine Return Parameters Local
Name Address Variables
Subroutine Return Parameters Local
Name Address Variables
www.pmt.education
When the subroutine GetTimeOfDaycompletes, its corresponding stack frame is popped
from the call stack:
Call Stack
Subroutine Return Parameters Local
Name Address Variables
Once the subroutine Greetinghas completed, the final stack frame is popped from the
call stack, leaving it empty.
Call Stack
www.pmt.education
Recursive techniques
A recursive subroutine is one which is defined in terms of itself. This means that
somewhere within the recursive subroutine, there is a call to the subroutine itself.
1 SUBROUTINE Factorial(Value)
2 IF Value = 0 THEN
3 RETURN 1
4 ELSE
5 RETURN Value * F actorial(Value - 1)
6 ENDIF
7 ENDSUBROUTINE
The algorithm can be called recursive because it calls itself on line 5 (shown in bold). The
algorithm’s base case is when Value = 0
. In this case, the algorithm doesn’t use
recursion to return a result.
When a problem can be solved recursively, it can often also be solved using iteration.
While iterative solutions are often easier to program, recursive solutions can be more
compact in code.
www.pmt.education
Iterative Binary Search Example
www.pmt.education