Programming Notes 2023
Programming Notes 2023
Data Types
A data type is a classification of data into groups according to the kind
of data they represent
Computers use different data types to represent different types of data
in a program
The basic data types include:
o Integer: used to represent whole numbers, either positive or
negative
Examples: 10, -5, 0
o Real: used to represent decimal numbers, either positive or
negative
Examples: 3.14, -2.5, 0.0
o Char: used to represent a single character such as a letter, digit
or symbol
Examples: 'a', 'B', '5', '$'
o String: used to represent a sequence of characters
Examples: "Hello World", "1234", "@#$%
o Boolean: used to represent true or false values
Examples: True, False
Python
Syntax variable_name = value
Example x = 5
It is important to choose the correct data type for a given situation to ensure
accuracy and efficiency in the program.
Worked Example
Name and describe the most appropriate programming data type for each of
the examples of data given. Each data type must be different[6]
Data: 83
Data type name: Integer [1 mark]
Data type description: The number is a whole number [1 mark]
Data: [email protected]
Data type name: String [1 mark]
Data type description: It is a group of characters [1 mark]
Data: True
Data type name: Boolean [1 mark]
Data type description: The value is True (or could be False) [1 mark]
Variables & Constants
Variables and constants are used to store data in a program. Variables can
be changed during program execution while constants remain the same.
Examples in Pseudocode:
score ← 10
const PI ← 3.14
Examples in Python:
score = 10
PI = 3.14
Exam Tip
Exam questions will ask you to write pseudocode statements, rather than in
a specific language
Worked Example
The variables 'name' and 'age' are used to store data in a program:
Pseudocode example:
Python example:
Input
Input refers to the process of providing data or information to a program.
Pseudocode example:
INPUT name
Python example:
Exam Tip
Remember to prompt the user for input clearly, and format the output to
make it readable and understandable.
Sequence
What is Sequence?
x←5
y←10
z← x + y
PRINT z
Python example:
print("Hello, World!")
x = 5
y = 10
z = x + y
print(z)
Exam Tip
Always remember to write your instructions in the order in which you want
them to be executed
Selection
Selection is a programming concept that allows you to execute different sets
of instructions based on certain conditions. There are two main types of
selection statements: IF statements and CASE statements.
If Statements
IF statements allow you to execute a set of instructions if a condition is true.
They have the following syntax:
IF condition THEN
instructions
ENDIF
Pseudocode example:
x← 5
IF x > 0 THEN
PRINT "x is positive"
ENDIF
Python example:
x = 5
if x > 0:
print("x is positive")
IF ELSE Statements
IF condition THEN
Instructions
ELSE
Instructions
ENDIF
Pseudocode example:
x ← 5
IF x > 0 THEN
PRINT "x is positive"
ELSE
PRINT “x is negative”
ENDIF
Python example:
x = 5
if x > 0:
print("x is positive")
else:
print("x is negative")
IF ELSE IF Statements
x ← 5
IF x > 0 THEN
PRINT "x is positive"
ELSE IF x < 0 THEN
PRINT “x is negative”
ELSE
PRINT “x is 0”
ENDIF
Python example:
x = 5
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is 0")
Worked Example
Write an algorithm using pseudocode that:
Inputs 3 numbers
Outputs the largest of the three numbers[3]
INPUT a, b, c
IF a > b AND a > c THEN PRINT a (1 mark)
ELSE IF b > c THEN PRINT b (1 mark)
ELSE PRINT c (1 mark)
Case Statements
match (number)
case 1:
print "Monday";
case 2:
print "Tuesday";
case 3:
print "Wednesday";
case 4:
print "Thursday";
case 5:
print "Friday";
case 6:
print "Saturday";
case 7:
print "Sunday";
case _:
Exam Tip
Iteration
Iteration is the process of repeating a set of instructions until a specific
condition is met. It is an important programming concept and is used to
automate repetitive tasks.
Count-controlled loops
Precondition loops
Postcondition loops
Count-controlled Loops
Example in Pseudocode:
count ← 1
FOR i <= 10
OUTPUT count
count ← count + 1
END FOR
Example in Python:
Example in Pseudocode:
INPUT temperature
WHILE temperature > 37
OUTPUT "Patient has a fever"
INPUT temperature
END WHILE
Example in Python:
Example in Pseudocode:
REPEAT
INPUT guess
UNTIL guess = 42
Example in Python:
Postcondition loops don’t exist in Python and would need to be
restructured to a precondition loop
Totalling
Pseudocode example:
total ← 0
for i ← 1 to 10
input num
total ← total + num
next i
output total
Python example:
total = 0
for i in range(1, 11):
num = int(input("Enter a number: "))
total += num
print("Total:", total)
Counting
Counting involves keeping track of the number of times a particular event
occurs
A count variable can be initialised to 0 and then updated within a loop, such
as:
Pseudocode example:
count ← 0
for i ← 1 to 10
input num
if num > 5 then
ount ← count + 1
end if
next i
output count
Python example:
count = 0
for i in range(1, 11):
num = int(input("Enter a number: "))
if num > 5:
count += 1
print("Count:", count)
Worked Example
Write an algorithm using pseudocode that:
Inputs 20 numbers
Outputs how many of these numbers are greater than 50 [3]
FOR x ← 1 TO 20 (1 mark)
INPUT Number
IF Number > 50 THEN Total ←
(1 mark)
Total + 1
NEXT x
PRINT total (1 mark)
String Handling
Length:
Substring:
Upper:
Lower:
Pseudocode example:
string phrase ← "Save my exams"
length ← LENGTH(phrase)
substring ← SUBSTRING(phrase, 5, 2)
upper ← TO_UPPER(phrase)
lower ← TO_LOWER(phrase)
Python example:
Worked Example
The function Length(x) finds the length of a string x. The function
substring(x,y,z) finds a substring of x starting at position y and z characters
long. The first character in x is in position 1.
Exam Tip
Remember that the length of a string is not the same as the index of
the last character in the string
Be careful when counting positions in a string, as the first character
can be at position zero or one depending on the programming
language
Arithmetic Operators
Pseudocode example:
a ←5
b ←3
c ←a + b
d ←a - b
e ←a * b
f ←a / b
g ← a MOD b
h ←a ^ b
i ← a DIV b
Python example:
a = 5
b = 3
c = a + b
d = a - b
e = a * b
f = a / b
g = a % b
h = a ** b
i = a // b
Logical Operators
Pseudocode example:
a ←5
b ←3
c ← (a = b)
d ← (a < b)
e ← (a <= b)
f ← (a > b)
g ← (a >= b)
h ← (a <> b)
Python example:
a = 5
b = 3
c = (a == b)
d = (a < b)
e = (a <= b)
f = (a > b)
g = (a >= b)
h = (a != b)
Boolean Operators
Boolean operators are logical operators that can be used to compare two or
more values and return a Boolean value (True or False) based on the
comparison. There are 3 you need to know:
Exam Tip
Nested Statements
Exam Tip
You will not be required to write more than three levels of nested statements
Selection
Pseudocode example:
if a > b then
if b > c then
output “a is the largest”
else
output “c is the largest”
else
if a > c then
output “b is the largest”
else
output “c is the largest”
Python example:
if a > b:
if b > c:
print("a is the largest")
else:
print("c is the largest")
else:
if a > c:
print("b is the largest")
else:
print("c is the largest")
Exam Tip
Indentation is key so make sure it's clear in your answer which if statement
line is part of which. It's more clear when you use end if in the appropriate
places, too.
Iteration
Nested iteration refers to a loop inside another loop.
Pseudocode example:
FOR i ← 1 TO 10
FOR j ← 1 TO 5
OUTPUT "i = ", i, " j = ", j
END FOR
END FOR
Python example:
Subroutines
Procedures and functions are types of subroutines. They are a sequence of
instructions that perform a specific task or set of tasks
Procedures
Pseudocode example:
PROCEDURE calculate_area(length: INTEGER, width: INTEGER)
area ← length * width
OUTPUT "The area is " + area
END PROCEDURE
To call the procedure you would use the following pseudocode:
calculate_area(5,3)
Python example:
Functions
Pseudocode example:
OUTPUT(calculate_area(5,3))
Python example:
A local variable is a variable that is declared inside a subroutine and can only be
accessed from within that subroutine
A global variable is a variable that is declared outside of a subroutine and can be
accessed from anywhere in the program
Global variables can be used to pass data between different subroutine
Overuse of global variables can make a program difficult to understand and debug and is
therefore discouraged as it’s considered bad practice
Pseudocode example:
global total_score
FUNCTION calculate_average(num1: INTEGER, num2: INTEGER)
average ← (num1 + num2) / 2
RETURN average
END FUNCTION
total_score ← 0
PROCEDURE add_score(score: INTEGER)
total_score ← total_score + score
END PROCEDURE
Python example:
Library Routines
Library routines are pre-written code that we can include in our programs to
perform specific tasks.
Using library routines saves us time by not having to write code from
scratch
Library routines are also tested and proven to work, so we don't have
to worry about errors as much
Some commonly used library routines include:
o Input/output routines
o Maths routines
Libraries can be included in our programs by importing them
The specific syntax for importing a library may vary depending on the
programming language being used
It is important to check the documentation for a library to understand
its usage and available functions
Maths routines
Exam Tip
Remember to import or include the appropriate library before using the
routines in your code
Maintaining Programs
Why is it important to create a maintainable program?
1D Arrays
Using Arrays
Arrays are used to store and manage multiple values of the same data
type efficiently
They can be used for tasks such as storing student scores, calculating
averages, and managing inventory data
Exam Tip
In pseudocode, the first index can be either 0 or 1, so it is crucial to read the
question to find out
Pseudocode example:
Worked Example
1. Declare an array to store customer names [1]
CustomerNames[1:30] [1 mark]
CustomerDOB[1:30]
CustomerTelNo[1:30]
CustomerEmail[1:30] [1 mark]
CustomerBalance[1:30] [1 mark]
Using Iteration
Loops can be used to write values into arrays, iterating through each
element and assigning a value
Iteration can also be used to read values from arrays, traversing
through each element and performing operations or outputting the
value
Nested iteration is useful for working with multi-dimensional arrays,
such as 2D arrays, where you need to iterate through rows and
columns
Pseudocode example:
FOR i FROM 0 TO 2
FOR j FROM 0 TO 2
OUTPUT scores[i][j]
ENDFOR
ENDFOR
Python example:
scores = [[0] * 3 for _ in range(3)]
for i in range(3):
for j in range(3):
scores[i][j] = int(input())
for i in range(3):
for j in range(3):
print(scores[i][j])
Exam Tip
Keep track of your loop variables to avoid confusion and errors - it's best to
give these clear names (row and column) rather than I and j
Worked Example
Write the pseudocode to output the contents of the
arrays Balance[ ] and Email [ ] along with suitable messages [3]
Count ← 0
REPEAT
PRINT "Customer: ", Count, " Balance: ", Balance[Count], "
Email: ",Email[Count] [3 marks]
Count ← Count + 1
UNTIL Count = 30 [1 mark]