1.1. Programming
1.1. Programming
1.1. Programming
3.1.1 Programming
Intermediate Notes
www.pmt.education
Specification:
www.pmt.education
3.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
3.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
3.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.
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.
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. Indefinite iteration is used when the number of repetitions required
is not known before the loop starts.
This is an example of definite iteration. The The whileloop above uses indefinite
forloop will run 64 times before finishing. iteration. The number of repetitions is not
known before the loop begins.
Nested Structures
Selection structures and iteration structures can be nested.
www.pmt.education
Meaningful Identifier Names
When declaring a variable, 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.
If a different programmer, who was unfamiliar with your program, were to read the code,
they should be able to work out the purpose of a constant, variable or subroutine from its
name.
Arithmetic Operations
Integer Division The same as real / float division, but just the 12 \ 8 = 1
whole number part is given. Or 12 DIV 8 = 1
www.pmt.education
Relational Operations
You can make use of relational operators whenever you need to compare two values.
They are used in ifstatements and whileloops to name a few examples.
Operation Example
Equal to 5=5
Boolean Operations
As explained earlier, a Boolean data type is one with a value that 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.
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
A built-in function takes a seed value and uses a series of mathematical operations to
arrive at a number.
It’s important that you make yourself familiar with random number generation in your
chosen programming language.
Exception handling
Once an exception has been thrown, the computer has to handle the exception to avoid
crashing. It does this by pausing execution of the program and saving the current state of
the program before running a section of code called a catch block.
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 restores 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
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.
www.pmt.education